Presentation On Exceptions in Java
Presentation On Exceptions in Java
What is an exception?
An exception is an abnormal condition that changes the normal flow
of control in a program
An exception is an event, which occurs during the execution of a
program, that disrupts the normal flow of the program's instructions
Types of Exception?
Types of Exception
1) Checked Exception
Checked exceptions are checked at compile-time. The classes that
extend Throwable class except RuntimeException and Error are known
as checked exceptions e.g.IOException, SQLException etc.
2) Unchecked Exception
The classes that extend RuntimeException are known as unchecked
exceptions e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not
checked at compile-time.
3) Error
Errors are not possible to handle in your program code
Exception Hierarchy
try Block
Java try block is used to enclose the code that might throw an exception. It
must be used within the method.
Java try block must be followed by either catch or finally block.
Syntax of java try-catch
try{
//code that may throw exception
}catch(Exception_class_Name ref){}
Syntax of try-finally block
try{
//code that may throw exception
}finally{}
catch Block
Java catch block is used to handle the Exception. It
finally Block
Java finally block is a block that is used to execute
Example of try-catch-finally
public class Test {
public static void main(String args[]){
try{
int data=25/0;
System.out.println(data);
}
catch(ArithmeticException e)
{System.out.println(e);}
finally
{System.out.println("finally block is always executed");}
System.out.println("rest of the code...");
}
}
throw keyword
The Java throw keyword is used to explicitly throw
an exception.
We can throw either checked or uncheked exception
in java by throw keyword. The throw keyword is
mainly used to throw custom exception.
Syntax of java throw keyword is given below.
throw exception;
throws keyword
The Java throws keyword is used to declare an exception. It gives an
throws keyword(contd.)
Case1: You handle the exception
In case you handle the exception, the code will be executed fine whether exception occurs during the
program or not.
import java.io.*;
class M{
void method()throws IOException{
throw new IOException("device error");
}
}
public class Testthrows2{
public static void main(String args[]){
try{
M m=new M();
m.method();
}catch(Exception e){System.out.println("exception handled");}
System.out.println("normal flow...");
} }
throws keyword(contd.)
You declare the exception
A)In case you declare the exception, if exception does not occur, the code will be executed fine.
B)In case you declare the exception if exception occures, an exception will be thrown at runtime because
throws does not handle the exception.
Program if exception does not occur
import java.io.*;
class M{
void method()throws IOException{
System.out.println("device operation performed");
}
}
class Testthrows3{
public static void main(String args[])throws IOException{//declare exception
M m=new M();
m.method();
System.out.println("normal flow...");
} }
throws keyword(contd.)
Program if exception occurs
import java.io.*;
class M{
void method()throws IOException{
throw new IOException("device error");
}
}
class Testthrows4{
public static void main(String args[])throws IOException{//declare exception
M m=new M();
m.method();
System.out.println("normal flow...");
}
}
throw
Java throw keyword is used to
exceptions.
declare an exception.
signature.
e.g.
public void method()throws
IOException,SQLException.
If the superclass method declares an exception, subclass overridden method can declare same,
subclass exception or no exception but cannot declare parent exception.
import java.io.*;
class Parent{
void msg()throws Exception{System.out.println("parent");}
}
class Child extends Parent{
void msg()throws ArithmeticException{System.out.println("child");}
public static void main(String args[]){
Parent p=new Child();
try{
p.msg();
}catch(Exception e){}
}
}
Custom Exception
If you are creating your own Exception that is known as custom
Thank You