unit 4
unit 4
String s = “abc";
a[10]=50; //ArrayIndexOutOfBoundsException
9/23/2024
Dr. K. V. Metre, SOCSET, ITM SLS Baroda
18
University
Hierarchy of Exception classes
try {
// Code that might throw an exception
}
catch (ExceptionType e)
{
// Code to handle exception
}
Dr. K. V. Metre, SOCSET, ITM SLS Baroda
9/23/2024 23
University
Syntax of Exception Handling Code
try The "try" keyword is used to specify a block where we should place an
exception code. It means we can't use try block alone. The try block must
be followed by either catch or finally.
catch The "catch" block is used to handle the exception. It must be preceded by
try block which means we can't use catch block alone. It can be followed
by finally block later.
finally The "finally" block is used to execute the necessary code of the program.
It is executed whether an exception is handled or not.
throw The "throw" keyword is used to throw an exception.
throws The "throws" keyword is used to declare exceptions. It specifies that there
may occur an exception in the method. It doesn't throw an exception. It is
always used with method signature.
9/23/2024 Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 28
Program without exception handling:
public class TryCatchExample1 {
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
the rest of the code is not executed (the rest of the code statement
is not printed).
9/23/2024
Dr. K. V. Metre, SOCSET, ITM SLS Baroda
29
University
Program with exception handling:
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{ System.out.println(“ Division by 0 error”); }
//rest code of the program
System.out.println("rest of the code...");
} }
Output:
Division by 0 error
rest of the code...
9/23/2024 Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 30
public class TryCatchExample3 {
public static void main(String[] args) {
try
{
int data=50/0; //may throw exception
// if exception occurs, the remaining statement will not exceute
Output:
Division by zero
9/23/2024 Dr. K. V. Metre, SOCSET, ITM SLS Baroda University 31
Exception using the parent class exception.
Output:
25 Dr. K. V. Metre, SOCSET, ITM SLS Baroda
9/23/2024 33
University
along with try block, we also enclose exception code in a catch
block.
public class TryCatchExample7 {
public static void main(String[] args) {
try {
int data1=50/0; //may throw exception
}
catch(Exception e) // handling the exception
{ // generating the exception in catch block
int data2=50/0; //may throw exception
}
System.out.println("rest of the code");
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
• Here, we can see that the catch block didn't contain the exception code. So,
enclose exception code within a try block and use catch block only to handle
the exceptions.
Syntax
try {
// Protected code
{ // Catch block }
{ // Catch block }
{ // Catch block }
9/23/2024
Dr. K. V. Metre, SOCSET, ITM SLS Baroda
37
University
Nested try block
• In Java, using a try block inside another try block is permitted.
It is called as nested try block.
• Every statement that we enter a statement in try block,
context of that exception is pushed onto the stack.
• the inner try block can be used to
handle ArrayIndexOutOfBoundsException while the outer try
block can handle the ArithemeticException (division by zero).