exception handling in java
TRANSCRIPT
Exceptions
Mr.Prasad Sawant Department Of Computer Science
Prof .Ramkurshana More Arts, Commerce and Science College Akurdi. Mobile :9665755707 Email:[email protected]
Blog:http://prasadmsawant.blogspot.com/
Agenda Define exception.Discuss what is meant by exception
handing.Describe the try,catch and finally
blocks.Examine multiple catch blocks.Explore nested try catch blocks.Explain the use of throw and throws
keywords.Create user defined exception.
What is exception?When an error is encountered
during the execution of a program an exception is said to have occurred.
Handling Exception…
IF B IS ZERO GO TO EROR
C=A/B
PRINT C
GO TO EXIT
ERROR:
DISPLAY “CODE CAUSING ERROR DUE TO DIVISION BY ZERO”
EXIT:
END
BLOCK THAT
HANDLES ERROR
HANDLING EXCEPTION IN JAVA
OBJECT
THROWABLEError Exception
AWT ERROR
Thread Death..
………
SQLException
ClassNotFoundException
………
System-Defined ExceptionRaised implicitly by system because
of illegal execution of program When cannot continue program
execution any more Created by Java System
automatically Exception extended from Error class
and RuntimeException class
[DivByZero.java]
IndexOutOfBoundsException : ◦ When beyond the bound of index in the object which
use index, such as array, string, and vector ArrayStoreException :
◦ When assign object of incorrect type to element of array NegativeArraySizeException :
◦ When using a negative size of array
NullPointerException : ◦ When refer to object as a null pointer
SecurityException : ◦ When violate security. Caused by security manager
IllegalMonitorStateException : ◦ When the thread which is not owner of monitor involves
wait or notify method
Programmer-Defined Exception
Exceptions raised by programmer
Check by compiler whether the exception handler for exception occurred exists or not◦If there is no handler, it is error
Sub class of Exception class
The exception handling modeltry
{
// code which is expected to throw an exception
}
Catch (exception e1)
{
/*
If exception is thrown in the try block ,is of type e1,then perform necessary action here
*/
}
Multiple catch blockClass catch22{ public static void main(String args[]) {
try {
String num=args[0];int numValue=Interger.parseInt(num);System.out.println(“The squre is ”+numValue*numvalue”);
} catch(ArrayIndexOutOfBoundException e1){ System.out.println(“No argument is given!”);}catch(NumberFormatException e2)
{ System.out.println(“Not a number “); } }}
C:>javac catch22 No argument is given!
C:>javac catch22 aNot a number
C:>javac catch22 3The squre is 9
Nested try-catch …try{ int num=args.length; try { int numValue=Integer.parseInt(args[0]); System.out.println(“The squre is “+numValue*numValue); } catch(NumberFormatExcption e1) { System.out.println(“Not a Number!”); }}catch (ArrayIndexOutOf BoundException e2){ System.out.println(“No arguments given!”);}….
C:>javac catch No argument is given!
C:>javac catch22 aNot a number
C:>javac catch22 3The squre is 9
Finally block The ‘finally ’block is guaranteed to run whether or not
an exception occurs. The finally clauses of a try catch block will always execute ,even if there are return statement in the try catch part .
Try block
finally Catch block
finally
No exception
Exception
Using throws and throwException are thrown with the help of the
‘throw’ keyword .The ‘throw’ keyword is used to indicate that an exception has occurred .The operand of throw is an object of any class that is derived from the class ’Throwable’.
The throws keyword is used to list the exception that method can throw.