EXCEPTION HANDLING
INTRODUCTION
An exception is an unexpected event that disrupts the normal flow of
a Java program during runtime. It represents an error condition which
can be handled by the application.
🔹 Importance of Exception Handling:
Helps in maintaining normal application flow, Improves code reliability
and readability, Facilitates debugging and error tracing, Enables
customized error messages for users.
BUILD IN SUPPORT JAVA
Java provides built-in support for exception handling through the
keywords try, catch, finally, throw, and throws, and all exceptions in
Java are derived from the Throwable class.
EAMPLE
Types of Java Exceptions
Try-Catch Block
• A try-catch block in Java is a mechanism to handle exception. The try
block contains code that might thrown an exception and the catch
block is used to handle the exceptions if it occurs.
• try {
• // Code that may throw an exception
• } catch (ExceptionType e) {
• // Code to handle the exception
•}
Internal working of try-catch
Block
• Java Virtual Machine starts executing the code inside the try block.
• If an exception occurs, the remaining code in the try block is skipped,
and the JVM starts looking for the matching catch block.
• If a matching catch block is found, the code in that block is executed.
• After the catch block, control moves to the finally block (if present).
• If no matching catch block is found the exception is passed to the JVM
default exception handler.
• The final block is executed after the try catch block. regardless of
whether an exception occurs or not.
Finally Block
• The finally block is used to execute important code regardless of whether an
exception occurs or not.
• finally block is always executes after the try-catch block. It is also used for resource
cleanup.
• try {
• // Code that may throw an exception
• } catch (ExceptionType e) {
• // Code to handle the exception
• }finally{
• // cleanup code
•}
Handling Multiple Exception
• We can handle multiple type of exceptions in Java by using multiple catch
blocks, each catching a different type of exception.
• try {
• // Code that may throw an exception
• } catch (ArithmeticException e) {
• // Code to handle the exception
• } catch(ArrayIndexOutOfBoundsException e){
• //Code to handle the anothert exception
• }catch(NumberFormatException e){
• //Code to handle the anothert exception
•}
Java final, finally and finalize
• final: The final is the keyword that can be used for immutability and
restrictions in variables, methods, and classes.
• finally: The finally block is used in exception handling to ensure that a
certain piece of code is always executed whether an exception occurs
or not.
• finalize: finalize is a method of the object class, used for cleanup
before garbage collection.
final Keyword
• The final keyword in Java is used with variables, methods, and also
with classes to restrict modification.
• Syntax:
• // Constant value
• final int a = 100;
finalize() Method
• The finalize() method is called by the Garbage Collector just before an
object is removed from memory.
• It allows us to perform clean up activity.
• Once the finalized method completes, Garbage Collector destroys
that object.finalize method is present in the Object class.
Class not found exception
InterruptedException
IOException
EXERCISE 1
EXERCISE 2