exception handling in java

13
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/

Upload: prasad-sawant

Post on 20-May-2015

7.327 views

Category:

Education


6 download

TRANSCRIPT

Page 1: Exception handling in Java

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/

Page 2: Exception handling in Java

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.

Page 3: Exception handling in Java

What is exception?When an error is encountered

during the execution of a program an exception is said to have occurred.

Page 4: Exception handling in Java

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

Page 5: Exception handling in Java

HANDLING EXCEPTION IN JAVA

OBJECT

THROWABLEError Exception

AWT ERROR

Thread Death..

………

SQLException

ClassNotFoundException

………

Page 6: Exception handling in Java

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]

Page 7: Exception handling in 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

Page 8: Exception handling in Java

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

Page 9: Exception handling in Java

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

*/

}

Page 10: Exception handling in Java

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

Page 11: Exception handling in Java

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

Page 12: Exception handling in Java

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

Page 13: Exception handling in Java

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.