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

Exception Handling

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

Exception Handling

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

Exception handling

The concept of Exceptions


• A mistake in the program can lead to an error which will
produce unexpected results
• An error may produce an incorrect output or may
terminate the execution of the program.
• There are two types of errors,
– Compile time errors
• Missing semicolons , brackets in classes and methods
• Use of undeclared variables
• Missing double quotes in strings etc..
– Runtime errors
• Dividing an integer by zero
• Accessing an element that is out of bounds of an array
• Passing a parameter that is not in a valid range or
value.
• 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 we want the program to continue with the execution of
the remaining code, then we should try to catch the
exception object thrown by the error condition and then
display an appropriate message for taking corrective
actions. This task is known as exception handling.
• Or
• The process of converting system error messages into
user friendly error message is known as Exception
handling.
• Exceptions in java can be categorized into two types:
– Checked exceptions : A checked exception is an exception
that occurs at the compile time, these are also called as
compile time exceptions.
– Unchecked exceptions : An unchecked exception is an
exception that occurs at the time of execution. These are
also called as Runtime Exceptions.
• The general syntax:
try
{
// Protected code
}
catch(ExceptionName e1)
{
// Catch block
• Exception object:
• An exception object is an instance of an exception class.
• It gets created and handed to the Java runtime when an
exceptional event occurred that disrupted the normal flow
of the application.
• This is called “to throw an exception” because in Java you
use the keyword “throw” to hand the exception to the
runtime.
• When a method throws an exception object, the runtime
searches the call stack for a piece of code that handles it.
General format:
try
{
// statements, some of which might throw an exception
}
catch ( SomeExceptionType exp )
{
// statements to handle // SomeExceptionType exceptions
}
// additional catch blocks (optional)
// finally block (optional, unless there are no catch blocks)
• A method catches an exception using a combination of
the try and catch keywords.
• The code which is prone to exceptions is placed in the
try block. When an exception occurs, that exception
occurred is handled by catch block associated with it.
• Every try block should be immediately followed either
by a catch block or finally block.
• A catch statement involves declaring the type of
exception you are trying to catch.
• If an exception occurs in protected code, the catch block
(or blocks) that follows the try is checked.
• If the type of exception that occurred is listed in a catch
block, the exception is passed to the catch block much as
an argument is passed into a method parameter.
• Eg: when you divide any number by zero then system generate / by zero so
this is not understandable by user so you can convert this message into user
friendly error message like Don't enter zero for denominator.
class ExcepTest
{
public static void main(String args[])
{
try
{
int a = 0;
int b = 7/a; // Divide by zero, will lead to exception
}
catch(ArithmeticException e)
{
System.out.println( e); // or
// System.out.println(Don't enter zero for denominator.);

} }
}
Output:
Exception thrown :java.lang.ArithmeticException: / by zero
Multiple catch blocks
• A try block can be followed by multiple catch blocks. The syntax
for multiple catch blocks looks like the following −
try
{
// Protected code
}
catch(ExceptionType1 e1)
{
// Catch block
}
catch(ExceptionType2 e2)
{
// Catch block
}
catch(ExceptionType3 e3)
{ // Catch block }
• The previous statements demonstrate three catch blocks,
but you can have any number of them after a single try.
• If an exception occurs in the protected code, the
exception is thrown to the first catch block in the list.
• If the data type of the exception thrown matches
ExceptionType1, it gets caught there. If not, the
exception passes down to the second catch statement.
• This continues until the exception either is caught or falls
through all catches.
class UncaughtException
{
public static void main(String args[])
{
try
{
// int a[]=new int[5];
int a=30/0;
}
catch (ArithmeticException e)
{
System.out.println("Exception thrown :" + e);
}
catch(ArrayIndexOutOfBoundsException e1)
{
System.out.println("Exception thrown :" + e1);
}
}
Output:
Exception thrown :java.lang.ArithmeticException: / by zero
Nested try statements
• Sometimes a situation may arise where a part of a block may cause one
error and the entire block itself may cause another error. In such cases,
exception handlers have to be nested. Syntax:
try
{
statement 1;
statement 2;
try
{
statement 1;
statement 2;
}
catch(Exception e)
{
}
}
catch(Exception e)
{
Eg:
class Excep6
{
public static void main(String args[])
{
try
{
try
{
int a[]=new int[5];
a[8]=200;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
int value = 200/0;
}
catch(ArithmeticException e1)
{
System.out.println(e1);
}
}
}

You might also like