Exception Handling Module 4
Exception Handling Module 4
Output:
Before Division
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:8)
• When an exception occurs within a method, the method
creates (throws) an object to describe the exception. This
object is called as Exception Object, which contains the
complete information about what went wrong here, ex: type of
exception, state of program, line number in the source where
this exception occurred and so on.
• After a method throws an object, it will be caught by the
block of code written to handle it. If you have not handled an
exception in your program, the exception object will be caught
by the runtime system and it handles it in its own way. i.e.,
default exception handling will come into picture which only
does two things
Print the error message in the console
Terminate the program
Exception Handling Methods
• Java exception handling is managed via five keywords: try,
catch, throw, throws, and finally.
• Program statements that you want to monitor for exceptions
are contained within a try block. If an exception occurs
within the try block, it is thrown.
• Your code can catch this exception (using catch) and handle
it in some rational manner.
• System-generated exceptions are automatically thrown by
the Java run-time system.
• To manually throw an exception, use the keyword throw.
• Any exception that is thrown out of a method must be
specified as such by a throws clause.
• Any code that absolutely must be executed after a try block
completes is put in a finally block.
• This is the general form of an exception-handling block:
try {
// block of code to monitor for errors
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
finally {
// block of code to be executed after try block ends
}
• Here, ExceptionType is the type of exception that has
occurred.
• The try-catch block is used to handle exceptions in Java.
Here's the syntax of try...catch block:
try {
statement; // generates an exception
}
catch(Exception_type e) {
statement; // processes the exception
}
• Here, we have placed the code that might generate an
exception inside the try block. Every try block is
followed by a catch block.
• When an exception occurs, it is caught by
the catch block. The catch block cannot be used without
the try block.
class Main {
public static void main(String[] args) {
try {
// code that generate exception
int divideByZero = 5 / 0;
System.out.println("Rest of code in try block");
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException => " +
e.getMessage());
}}}
Output : ArithmeticException => / by zero
• In the example, we are trying to divide a number
by 0. Here, this code generates an exception.
• To handle the exception, we have put the code, 5
/ 0 inside the try block. Now when an exception
occurs, the rest of the code inside the try block is
skipped.
• The catch block catches the exception and
statements inside the catch block is executed.
• If none of the statements in the try block
generates an exception, the catch block is
skipped.
Java finally block
• In Java, the finally block is always executed no matter
whether there is an exception or not.
• The finally block is optional. And, for each try block, there
can be only one finally block.
• The basic syntax of finally block is:
try {
//code
}
catch (ExceptionType1 e1) {
// catch block }
finally {
// finally block always executes
}
• If an exception occurs, the finally block is executed after
the try...catch block. Otherwise, it is executed after the try block.
For each try block, there can be only one finally block.
class Main {
public static void main(String[] args) {
try {
// code that generates exception
int divideByZero = 5 / 0;
}
catch (ArithmeticException e) {
System.out.println("ArithmeticException => " + e.getMessage());
}
finally { System.out.println("This is the finally block"); } } }
Output
ArithmeticException => / by zero
This is the finally block
public class Main {
public static void main(String[ ] args) {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // error!
}
}