exception with previous paper question on exception
exception with previous paper question on exception
v=bMhDwdT5AHw
Java provides a way to handle these exceptions so your program doesn’t crash suddenly.
This is called exception handling.
This line catches the exception thrown from the try block if it's an ArithmeticException
— for example, dividing by zero.
📘 catch:
💡 What is e?
You can name it anything (e, ex, error, etc.), but e is commonly used.
Example
🧠 Summary:
Simple Example:
🧠 What happens:
• try block:
It contains code that might throw an exception. If an exception occurs, the rest of
the code in the try block is skipped, and control is transferred to the catch block.
• catch block:
This block handles the exception. You can write code here to show a message, log
the error, or recover from the problem.
• finally block:
This block always executes, whether an exception occurred or not. It is often used
to close files, release resources, or clean up code.
• The throw keyword is used to manually throw an exception (either built-in or user-
defined).
• It is typically used for custom validation when you want to signal an error condition
yourself.
Exceptions are for problems you can handle (like divide-by-zero or file not found).
Errors are for serious problems you usually can’t fix in code (like running out of memory).