0% found this document useful (0 votes)
6 views

Exception Handling in Java Day-2

The document discusses exception handling in Java. It covers try/catch blocks, different types of exceptions, and using multiple catch clauses. Exceptions represent runtime errors, and catch blocks allow handling exceptions gracefully instead of program termination.

Uploaded by

Ayush Seth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Exception Handling in Java Day-2

The document discusses exception handling in Java. It covers try/catch blocks, different types of exceptions, and using multiple catch clauses. Exceptions represent runtime errors, and catch blocks allow handling exceptions gracefully instead of program termination.

Uploaded by

Ayush Seth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Exception Handling in Java

Revision
• An Exception is a run-time error
• To monitor code for exceptions must part of try
block.
• The catch receives exceptions. A catch is not
called; thus execution does not return to the
point at which the exception was generated.
• Rather, execution continues after the catch block.
• The type of exception must match the type
specified in a catch.
Exception Handling
class Exp {
static void genExp() {
int []num=new int[4];
System.out.println("\n Before exception");
num[7]=10; //Exception Generated here
System.out.println("\n After exception");
}
}
class DemoExp
{
public static void main(String args[])
{
try
{
Exp.genExp(); // Static method ,so need to create object
}
catch(ArrayIndexOutOfBoundsException ex) //Exception caught here
{
System.out.println("\n Index Out Of Bounds");
}
System.out.println("\n After Exception");
}
}
Types of Exceptions
Find the output
class TestExp
{
public static void main(String args[])
{
int []num=new int[6];
try
{
System.out.println("\n Before Exception");
num[7]=10;
System.out.println("\n After Exception");
}
catch(ArithmeticException ex)
{
System.out.println("\n Index Out Of Bounds");
}
System.out.println("\n After Exception");
}
}
Handle Errors Gracefully
class TestExp
{
public static void main(String args[])
{
int []num1={4,8,16,32,64,128};
int []num2={2,0,4,4,0,8};
for(int i=0;i<num1.length;i++)
{
try
{
System.out.println(num1[i]+"/"+num2[i]+" is "+num1[i]/num2[i]);
} C:\prac-java>java TestExp
catch(ArithmeticException ex)
{
4/2 is 2
System.out.println("\n can't divide by zero");
} can't divide by zero
} 16/4 is 4
} 32/4 is 8
}

can't divide by zero


128/8 is 16
Points to Remember
• The type of exception in a catch must match
the type of exception that we want catch.
• An uncaught exception ultimately leads to
abnormal program termination.
• A program should handle exceptions in a
rational, graceful manner, eliminating the
cause of the exception if possible and then
continuing.
Using Multiple catch Clauses
• We can associate more than one catch clause
with a single try.
• Each catch must catch a different type of
exception.
• In general catch clauses are checked in the
order in which they occur in a program.
• Only a matching clause is executed, all other
catch blocks are ignored.
Example
class TestExp
{
public static void main(String args[])
{
// Here length of num1>num2
int []num1={4,8,16,32,64,128,256,512};
int []num2={2,0,4,4,0,8};
for(int i=0;i<num1.length;i++)
{
try
{
System.out.println(num1[i]+"/"+num2[i]+" is "+num1[i]/num2[i]);
}
catch(ArithmeticException ex)
{
System.out.println("\n can't divide by zero");
}
catch(ArrayIndexOutOfBoundsException ex)
{
System.out.println("\n No matching element found");
}
}

You might also like