Quote
"If you can change your mind, you can change your life."
Exception Handling
An exception is an unwanted or unexpected event that occurs during the execution of a program.
For example: runtime errors like dividing a number by zero.
To handle such situations, we use Exception Handling, which provides an alternative way to continue the normal flow of the program instead of stopping it abruptly.
Exception Herarichy
Difference between Exception and Error
Types of Exceptions in Java
1.CompileTime Exception or (Checked Exception)
2.Runtime Exception or (Unchecked Exception)
CompileTime Exception or (Checked Exception)
- Exception Checked by the Compiler and handled with (try-catch throws).
- A checked exception is an exception that the Java compiler checks at compile-time.
👉 Examples of checked exceptions:
IOException
-
SQLException
-
FileNotFoundException
One way to handle checked exceptions is by catching and handling them inside the method itself using atry-catch
block.
🔎 Explanation:
-
FileReader
tries to open test.txt. -
If the file does not exist, a
FileNotFoundException
(checked exception) is thrown. -
The
try-catch
block catches the exception and prints an error message instead of stopping the program. -
This way, the program handles the exception inside the method itself — no need to declare
throws.
- While Compiler not checked by compiler, Occurs during Program execution.
We can write code with runtime exceptions without handling them.
-
If they occur, they happen during program execution (runtime), not at compile time.
We can Handle the Exception using 5 keywords.
1.try
2.catch
3.finally
4.throw
5.throws
try-catch
Syntax of try-catch
try {
// Risky Code
} catch (ExceptionType e) {
// Code to handle the exception
}
Note: For print exception() methods.
catch (ExceptionType e)
- exceptionName
- description
- stackTrace(line)
Example:
class TryCatchExample {
public static void main(String[] args) {
try {
int number = 10;
int result = number / 0;
// This will throw ArithmeticException
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
}
}
}
Output:
Error: Cannot divide by zero.
-
try
block → Place the code that might throw an exception. -
catch
block → Handles the exception if it occurs. -
If no exception occurs →
catch
block is skipped.
- A
finally
block always runs aftertry
and any matchingcatch
blocks — used for cleanup (closing files, releasing locks, etc.) - It runs whether an exception was handled or not.
finally
for a try
block. catch
blocks are optional when you have finally
.finally
runs even though no exception happened.System.exit()
is calledfinally
is skipped because JVM shuts down.- If the program crashes due to a native error or external system failure (e.g., OutOfMemoryError in some cases, or hardware failure), the
finally
block won’t run.
try
finally
never executes because control never leaves the try
.- Actualy throwing Exception Object.
- Customize Exception.
- Used inside method to throw exception object.
- The
throw
keyword is used to manually create an exception object and pass it to the JVM for handling. - We can't write any statement after throw.
- Exception handling by try-catch.
- We can throw only one exception at a time using
throw
. - Once thrown, the JVM passes that object to the runtime system to handle it.
- It is used to declare that a method may throw exception.
- It is Keyword (in method signature).
- It does not handle the exception itself, but passes the responsibility to the caller.
- The caller must handle it (with
try-catch
) or declare it again usingthrows
.
No comments:
Post a Comment