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

EXCEPTION

The document discusses exception handling in Java. It explains that exceptions are errors that occur during program execution and divert normal program flow. It describes different types of exceptions in Java and how they are handled using try, catch, and finally blocks.

Uploaded by

Yash Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

EXCEPTION

The document discusses exception handling in Java. It explains that exceptions are errors that occur during program execution and divert normal program flow. It describes different types of exceptions in Java and how they are handled using try, catch, and finally blocks.

Uploaded by

Yash Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Every programming language consists of semantic

faults. To handle these semantic faults there


should be appropriate code to handle them. If the
programmed is not prepared for these faults the
entire software development may be crashed.
Java uses the concept of exception handling to
handle these semantic faults.

Exception is a predefined class present in java.lang


package. Exception is an abnormal condition that occurs
during program execution and diverts normal flow of
instructions. The situation that may occur during program
execution are :

 The array index crosses the range


 A number is divided by zero
 Unable to find the file
 Problems in network connectivity
In Java Exceptions are handled by five keywords
1.try 2. catch 3. finally 4.throw 5. throws
The Exception is placed in a try block and catch
block is used to handle the Exception.
If some necessary code is to be executed after try
then it is placed inside finally block.
try
{
// Block of code to support exception
}
catch (Exception except1)
{
// code to handle the exception
}
finally
{
// the code to be executed must
}
Types of Exception
1. Checked Exception
2. Unchecked Exception
3. Chained Exception
Checked Exception
These are checked by the compiler. If the
programmer generates a checked exception
within a method with the help of throw
keyword then the programmer is bound is
bound to handle the exception within that
block otherwise the program terminates at the
compile time.
If the programmer generates a checked
exception and not handles it within that block
where the exception is generated, then the
programmer pass the exception to the caller of
the methods by the help of throws keyword.

public class checked


{
void show()
{
try { throw new Exception();}
catch (Exception e)
{
System.out.println("Java");
}
}
void display() throws Exception
{
throw new Exception();
}
public static void main(String args[])
{
checked c = new checked();
c.show();
try {
c.display();
}
catch (Throwable t)
{
System.out.println("Interaface");
}
}
}

Output:

Java
interface
show() is a method which generates a checked
exception and handles the exception within
show() method

display() is a method which generates a


checked exception and but doesnot handle the
it within display() method and passes the
exception to the caller by throw keyword
EXCEPTIONS
An exception is a condition that is caused by a
run time error in the program. When the java
interpreter encounters an error such as
dividing an integer by zero, it creates an
exception object and throws it (i.e. informs us
that an error has occurred.
If the exception object is not caught and
handled properly, the interpreter will display
an error message and will terminate the
program. If we want the program to continue
with the execution of remaining code, then we
should try to catch the exception object thrown
by the error condition and then display an
appropriate msg for taking corrective actions.
This task is known as exception handling.
The purpose of exception handling mechanism
is to provide a means to detect and report an
“exceptional circumstances” so that
appropriate action can be taken. The
mechanism suggests incorporation of a
separate error handling code that performs the
foll tasks:
1>Find the problem(Hit the exception).
2>Inform that an error has occurred(Throw the
exception).
3>Receive the error information (Catch the
exception).
4>Take corrective actions(Handle the
exception).
The error handling code basically consists of
two segments,one to detect error and th throw
exceptions and the other to catch exceptions
and to take appropriate actions.
When writing programs ,we must always be on
the lookout for places in the program where an
exception could be generated.
Java uses a keyword try to preface a block of
code that is likely to cause an error condition
and “throws” an exception. A catch block
defined by the keyword catch “catches” the
exception “thrown” by three try block and
handles it appropriately. The catch block is
added immediately after the try block. The
following example illustrates the use of simple
try and catch statements:

…………….
…………….
try
{
Statement; //generates an exception
}
catch (Exception-type e)
{
Statement; //processes the exception
}
……………
……………

>Program using try and catch for exception


handling:

Class error3
{
public static void main(String args[])
{
int a=10;
int b=5;
int c=5;
int x,y;
try
{
x=a/(b-c); //Exception here
}
catch(Exception e)
{
System.out.println(“Division by zero”);
}
y=a/(b+c);
System.out.println(“y=”+y);
}
}

Output:-Division by zero
y=1

>Program for nested try statements:

class egnestedtry
{
public static void main(String args[])
{
try
{
int a=2,b=4,c=2,x=7,z;
int p[]={2};
p[3]=33;
try
{
Z=x/((b*b)-(4*a*c));
System.out.println(“The value of z is”=+z);
}
catch(ArithematicException e)
{
System.out.println(“Division by zero in
arithematic expression);
}
}
catch(ArrayIndexOutOfBoundException e)
{
System.out.println(“Array index is out-of-bond”
);
}
}
}

Output:Array index is out-of-bound

Multiple catch statements:


It is possible to have more than one catch
statements in the catch block as illustrated
below:
{
try
{
Statement; //generates an exception
}
catch(Exception-type-1 e)
}
Statement; //processes exception type 1
}
catch(Exception-typt-2 e)
{
Statement; //processes exception-type-2
}
.
.
.
catch(Exception-type-n e)
{
Statement; //processes exception type n
}
………….
………….
>program using multiple catch blocks:

class error4
{
int a[]={5,10};
int b=5;
try
{
int x=a[2]/b-a[1];
}
catch(ArithematicException e)
}
System.out.println(“Division by zero”);
}
catch(ArrayIndexOfBoundsException e)
{
System.out.println(“Array index error”);
}
catch(ArrayStoreException e )
{
System.out.println(“Wrong data type”);
}
int y=a[1]/a[10];
System.out.println(“y=”+y);
}
}
Output:Array index error
y=2

You might also like