https://www.youtube.com/watch?
v=bMhDwdT5AHw
🧠 What is Exception Handling?
When your Java program runs, sometimes things can go wrong (like dividing by zero, or
opening a file that doesn’t exist). These errors at runtime are called exceptions.
Java provides a way to handle these exceptions so your program doesn’t crash suddenly.
This is called exception handling.
🔑 Keywords used in Exception Handling:
1. try – Block where you write code that may cause an exception.
2. catch – Block that handles the exception.
3. finally – Block that always runs (whether exception happens or not).
4. throw – Used to throw an exception manually.
5. throws – Declares exceptions that a method might throw.
Simple Example:
💡 What is catch (ArithmeticException e)?
This line catches the exception thrown from the try block if it's an ArithmeticException
— for example, dividing by zero.
Let’s explain it part by part:
📘 catch:
• Used to handle exceptions.
• It follows a try block.
• It "catches" the exception if it matches the specified type.
catch (ArithmeticException e) means:
"If an ArithmeticException happens in the try block, catch it and use the object e to
understand or handle the error."
what e does ?
Let’s explain what e does in a catch block like this:
💡 What is e?
• e is just a variable name.
• It holds the exception object that was thrown.
• You can use it to get details about the error.
You can name it anything (e, ex, error, etc.), but e is commonly used.
Example
🧠 Summary:
e is the object that represents the exception that happened.
You use it to inspect or log the error.
throw – Throw an exception manually
🧠 What happens:
• If age < 18, we manually throw an ArithmeticException.
• The program stops there unless handled with a try-catch.
throws – Tell Java that the method might throw an exception
Simple Example:
🧠 What happens:
• readFile() might throw a FileNotFoundException, which is a checked
exception.
• So we declare throws IOException to inform Java:
“Hey, this method might throw an exception. Be ready to handle it.”
📌 Bonus Tip:
You can combine both:
IMORTANT PREVIOUS YESR QUESTION
i. Use of try-catch and finally block in Exception Handling
• 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.
Java Program: Handling ArrayIndexOutOfBoundsException using try-catch-
finally
✳ Purpose of the throw Keyword in Java
• 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.
Java Program with Comments: MarksOutOfBoundsException
Difference Between Error and Exception in Java
✅ Summary:
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).