Exception handling in Java

8,361 views
8,149 views

Published on

Published in: Education
1 Comment
3 Likes
Statistics
Notes
No Downloads
Views
Total views
8,361
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
279
Comments
1
Likes
3
Embeds 0
No embeds

No notes for slide

Exception handling in Java

  1. 1. Exceptions Mr.PrasadSawantDepartment Of Computer ScienceProf .Ramkurshana More Arts, Commerce and Science College Akurdi. Mobile :9665755707 Email:sawanprasad@gmail.comBlog:http://prasadmsawant.blogspot.com/<br />
  2. 2. Agenda <br />Define exception.<br />Discuss what is meant by exception handing.<br />Describe the try,catch and finally blocks.<br />Examine multiple catch blocks.<br />Explore nested try catch blocks.<br />Explain the use of throw and throws keywords.<br />Create user defined exception.<br />
  3. 3. What is exception?<br />When an error is encountered during the execution of a program an exception is said to have occurred.<br />
  4. 4. Handling Exception<br />…<br />IF B IS ZERO GO TO EROR<br />C=A/B<br />PRINT C<br />GO TO EXIT <br /> ERROR:<br /> DISPLAY “CODE CAUSING ERROR DUE TO DIVISION BY ZERO”<br />EXIT:<br />END<br />BLOCK THAT HANDLES <br />ERROR<br />
  5. 5. HANDLING EXCEPTION IN JAVA <br />OBJECT<br />Error<br />THROWABLE<br />Exception<br />SQLException<br />AWT ERROR<br />ClassNotFoundException<br />Thread Death..<br />………<br />………<br />
  6. 6. System-Defined Exception<br />Raised implicitly by system because of illegal execution of program <br />When cannot continue program execution any more <br />Created by Java System automatically <br />Exception extended from Error class and RuntimeException class <br /><ul><li> [DivByZero.java] </li></li></ul><li>IndexOutOfBoundsException: <br />When beyond the bound of index in the object which use index, such as array, string, and vector <br />ArrayStoreException : <br />When assign object of incorrect type to element of array <br />NegativeArraySizeException : <br />When using a negative size of array <br />NullPointerException :<br />When refer to object as a null pointer<br />SecurityException : <br />When violate security. Caused by security manager<br />IllegalMonitorStateException : <br />When the thread which is not owner of monitor involves wait or notify method <br />
  7. 7. Programmer-Defined Exception <br />Exceptions raised by programmer <br />Check by compiler whether the exception handler for exception occurred exists or not<br />If there is no handler, it is error <br />Sub class of Exception class <br />
  8. 8. The exception handling model<br />try <br />{ <br /> // code which is expected to throw an exception <br />}<br />Catch (exception e1)<br />{<br /> /*<br /> If exception is thrown in the try block ,is of type e1,then perform necessary action here <br /> */<br />}<br />
  9. 9. Multiple catch block<br />Class catch22<br />{<br /> public static void main(String args[])<br /> {<br /> try<br /> {<br /> String num=args[0];<br />intnumValue=Interger.parseInt(num);<br />System.out.println(“The squre is ”+numValue*numvalue”);<br /> }<br /> catch(ArrayIndexOutOfBoundException e1)<br /> {<br />System.out.println(“No argument is given!”);<br /> }<br /> catch(NumberFormatException e2) <br /> { <br />System.out.println(“Not a number “);<br /> }<br /> }<br />}<br />C:>javac catch22<br /> No argument is given!<br />C:>javac catch22 a<br />Not a number<br />C:>javac catch22 3<br />The squre is 9<br />
  10. 10. Nested try-catch <br />…<br />try<br />{<br />int num=args.length;<br /> try <br /> {<br />intnumValue=Integer.parseInt(args[0]);<br />System.out.println(“The squre is “+numValue*numValue);<br /> } <br /> catch(NumberFormatExcption e1)<br /> {<br />System.out.println(“Not a Number!”);<br /> }<br />}<br />catch (ArrayIndexOutOfBoundException e2)<br />{<br />System.out.println(“No arguments given!”);<br />}<br />….<br />C:>javac catch <br /> No argument is given!<br />C:>javac catch22 a<br />Not a number<br />C:>javac catch22 3<br />The squre is 9<br />
  11. 11. Finally block<br />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 . <br />Try block<br />No exception<br />Exception<br />finally<br />Catch block<br />finally<br />
  12. 12. Using throws and throw<br />Exception 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’.<br />The throws keyword is used to list the exception that method can throw.<br />

×