Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Shashwat Shriparv
dwivedishashwat@gmail.com
InfinitySoft
1
Exception Handling in Java
throw
It is possible to throw an exception explicitly.
Syntax:
throw ThrowableInstance
throwableInstance must be an object of type Throwable or a
subclass of Throwable.
By 2 ways v can obtain a Throwable object
1.Using parameter into a catch clause
2.Creating one with new operator
2
class throwDemo
{
public static void main(String s[])
{
int size;
int arry[]=new int[3];
size=Integer.parseInt(s[0]);
try
{ new operator used
if(size<=0)
throw new NegativeArraySizeException("Illegal Array size");
for(int i=0;i<3;i++)
arry[i]+=i+1;
parameter used into catch clau
}
catch(NegativeArraySizeException e)
{
System.out.println(e);
throw e; //rethrow the exception
}
}
} 3
throws
If a method causing an exception that it doesn't
handle, it must specify this behavior that callers of the
method can protect themselves against the exception.
This can be done by using throws clause.
throws clause lists the types of exception that a
method might throw.
Form
type methodname(parameter list) throws Exception list
{//body of method}
4
import java.io.*;
class ThrowsDemo
{
psvm(String d[])throws IOException,NumberFormatException
{
int i;
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(in);
i=Integer.parseInt(br.readLine());
System.output.println(i);
}}
5
finally
It creates a block of code that will b executed after try/catch block has
completed and before the code following try/catch block.
It will execute whether or not an exception is thrown
finally is useful for:
• Closing a file
• Closing a result set
• Closing the connection established with db
This block is optional but when included is placed after the last catch
block of a try
6
Form:
try
{}
catch(exceptiontype e) try block
{}
finally
{} finally Catch block
finally
7
8
Java Exception Type Hierarchy
Object
Throwable
Error Exception
RunTimeException
LinkageError ArithmeticException
IndexOutOfBoundsException
ThreadDeath StringIndexOutOfBoundsException
IllegalArguementException
VirtualMachineError NumberFormatException
IllegalAccessException
AWTError NoSuchMethodException
10
ClassNotFoundException
11
Unchecked Exceptions
ArithmeticException
ArrayIndexOutOfBoundsException
ClassCastException
IndexOutOfBoundsException
IllegalStateException
NullPointerException
SecurityException
12
Unchecked Exceptions
Exception Meaning
ClassNotFoundException
CloneNotSupportedException
IllegalAccessException
InstantiationException
InterruptedException
NoSuchFieldException
NoSuchMethodException
14
Checked Exceptions
Exception Meaning
15
Java’s Built-in Exceptions: inside java.lang package
16
• With an unchecked exception, however, the compiler
doesn't force client programmers either to catch the
exception or declare it in a throws clause.
• In fact, client programmers may not even know that
the exception could be thrown.
• eg, StringIndexOutOfBoundsException thrown by
String's charAt() method.
• Checked exceptions must be caught at compile time.
Runtime exceptions do not need to be.
17
Creating our own Exception class
For creating an exception class our own simply make
our class as subclass of the super class Exception.
Eg:
class MyException extends Exception
{
MyException(String msg)
{
super(msg);
}
}
18
class TestMyException
{
public static void main(String d[])
{
int x=5,y=1000;
try
{
float z=(float)x/(float)y;
if(z<0.01)
{
throw new MyException("too small number");
}
}
19
catch(MyException me)
{
System.out.println("caught my exception");
System.out.println(me.getMessage());
}
finally
{
System.out.println("from finally");
}
} Output
Output
}
• E:\JAVAPGMS>java TestMyException
• caught my exception
• too small number
• from finally
20
Summary
Objective
Objectiveof
ofException
ExceptionHandling
Handling
21
Thank you
Shashwat Shriparv
dwivedishashwat@gmail.com
22
InfinitySoft