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

Java3 8

Notes

Uploaded by

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

Java3 8

Notes

Uploaded by

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

Exception Handling

An exception is an abnormal condition that


arises in a code sequence at run time. In
other words, an exception is a run-time error
such as dividing an integer by zero.
When an exceptional condition arises, an
object representing that exception is created
and thrown.
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 the remaining
code ,then we should catch the exception
object and display appropriate message for
taking corrective actions.
this task is known as exception handling
common Java exceptions
ArithmeticException caused by math
errors such as division by zero
ArrayIndexOutOfBoundsException caused
by bad array indexes.
FileNotFoundException caused by an
attempt to access a nonexistent file
IOexception caused by I/O failures

All exception types are subclasses of the


built-in class Throwable.
exceptions in java are categorized into two
types:
checked exceptions -Exceptions are
explicitly handled in the code itself with the
help of try –catch blocks.
FileNotFoundException and IOexception are
checked exceptions
unchecked exceptions
these exceptions are not handled in the
program code instead JVM handle such
exceptions.
ArithmeticException,ArrayIndexOutOfBounds
Exception are unchecked exceptions
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.
A catch block defined by the keyword catch
catches the exception and handles it.
General form:
........
..........
try
{
statement; // generates an exception
}
catch (Exception-type e)
{
statement; // processes the exception
}
if the parameter of the catch statement
matches the type of the exception object then
the exception is caught and statements in the
catch block will be executed. otherwise the
exception is not caught and default exception
handler will terminate the execution of the
program
Program to demonstrate Arithmetic
Exception
class Exp1{
public static void main(String args[]) {
int d, a;
try { // monitor a block of code.
d = 0;
a = 42 / d;
}
catch (ArithmeticException e) {
System.out.println("Division by zero.");
}
System.out.println("After catch statement.");
}
}

You might also like