Java Exception Handling Handling errors using Java’s exception handling mechanism.
Exception handling
-
Upload
shashwat-shriparv -
Category
Technology
-
view
13 -
download
4
description
Transcript of Exception handling
![Page 2: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/2.jpg)
2
Exception Handling in Java
throwIt 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
![Page 3: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/3.jpg)
3
class throwDemo{public static void main(String s[]){int size;int arry[]=new int[3];size=Integer.parseInt(s[0]);try{if(size<=0) throw new NegativeArraySizeException("Illegal Array size");for(int i=0;i<3;i++)arry[i]+=i+1;}catch(NegativeArraySizeException e){System.out.println(e);throw e; //rethrow the exception}}}
new operator used
parameter used into catch clause
![Page 4: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/4.jpg)
4
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}
![Page 5: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/5.jpg)
5
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);
}}
![Page 6: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/6.jpg)
6
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
![Page 7: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/7.jpg)
7
Form:
try
{}
catch(exceptiontype e)
{}
finally
{}finally
finally
Catch block
try block
![Page 8: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/8.jpg)
8
![Page 9: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/9.jpg)
Java Exception Type Hierarchy
![Page 10: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/10.jpg)
10
Object
Throwable
Error Exception
LinkageError
ThreadDeath
VirtualMachineError
AWTError
RunTimeException
ArithmeticException
IndexOutOfBoundsException
IllegalArguementException
IllegalAccessException
NoSuchMethodExceptionClassNotFoundException
NumberFormatException
StringIndexOutOfBoundsException
![Page 11: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/11.jpg)
11
![Page 12: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/12.jpg)
12
ArithmeticException ArrayIndexOutOfBoundsException ClassCastException IndexOutOfBoundsException IllegalStateException NullPointerException SecurityException
The types of exceptions that need not be included in a method’s throws list are called Unchecked Exceptions.
Unchecked Exceptions
![Page 13: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/13.jpg)
13
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException
ClassCastException
IllegalArgumentException
IllegalMonitorStateException
IllegalStateException
IllegalThreadStateException
IndexOutOfBoundsException
NegativeArraySizeException
Exception
Arithmetic error, such as divide-by-zero.
Array index is out-of-bounds.
Assignment to an array element of an incompatible type.
Invalid cast.
Illegal argument used to invoke a method.
Illegal monitor operation, such as waiting on an unlocked
thread.
Environment or application is in incorrect state.
Requested operation not compatible with current thread state.
Some type of index is out-of-bounds.
Array created with a negative size.
Meaning
Unchecked Exceptions
![Page 14: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/14.jpg)
14
ClassNotFoundException CloneNotSupportedException IllegalAccessException InstantiationException InterruptedException NoSuchFieldException NoSuchMethodException
The types of exceptions that must be included in a method’s throws list if that method can generate one of these exceptions and does not handle it ,are called Checked Exceptions.
Checked Exceptions
![Page 15: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/15.jpg)
15
Checked Exceptions
Class not found.
Attempt to clone an object that does not implement the
Cloneable interface.
Access to a class is denied.
Attempt to create an object of an abstract class or interface.
One thread has been interrupted by another thread.
A requested field does not exist.
A requested method does not exist.
ClassNotFoundException
CloneNotSupportedException
IllegalAccessException
InstantiationException
InterruptedException
NoSuchFieldException
NoSuchMethodException
Exception Meaning
![Page 16: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/16.jpg)
• A checked exception is any subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.
• Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown.
• eg, IOException thrown by java.io.FileInputStream's read() method .
• Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked.
16
Java’s Built-in Exceptions: inside java.lang package
![Page 17: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/17.jpg)
• 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
![Page 18: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/18.jpg)
18
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);
}
}
![Page 19: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/19.jpg)
19
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");}}
![Page 20: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/20.jpg)
20
catch(MyException me){System.out.println("caught my exception");System.out.println(me.getMessage());}finally{System.out.println("from finally");}}}
• E:\JAVAPGMS>java TestMyException• caught my exception• too small number• from finally
OutputOutput
![Page 21: Exception handling](https://reader033.fdocuments.in/reader033/viewer/2022061105/54086f4ddab5ca5b348b53c6/html5/thumbnails/21.jpg)
21
Making safer program by providing special mechanism
Objective of Exception HandlingObjective of Exception Handling
Summary