Exeption handling

535 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
535
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Exeption handling

  1. 1. Disclaimer:This presentation is prepared by trainees of baabtraas a part of mentoring program. This is not official document ofbaabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  2. 2. Exception Handling vineethmenon18@gmail.com
  3. 3. Exception Handling in JavaTypes of Errors1.Compile timeAll syntax errors identified by java compiler.No class file is created when this occurs.So it is necessary to fix all compile time errors for successfulcompilation.Egs:Missing of semicolon,use of = instead of == 4
  4. 4. Exception Handling in Java2.Run timeSome pgms may not run successfully due to wrong logic orerrors like stack overflow.Some of the Common run time errors are: Division by 0 Array index out of bounds Negative array size etc.. 5
  5. 5. Exception Handling in JavaException is a condition caused by a run time error in the program.When the java interpreter identifies an error such as division by 0it creates an Exception object and throws itDefinition:An exception is an event, which occurs during the execution of aprogram, that disrupts the normal flow of the programsinstructions.Is defined as a condition that interrupts the normal flow ofoperation within a program. 6
  6. 6. Exception Handling in JavaJava allows Exception handling mechanism to handle variousexceptional conditions. When an exceptional condition occurs, anexception is said to be thrown.For continuing the program execution, the user should try to catchthe exception object thrown by the error condition and then displayan appropriate message for taking corrective actions. This task isknown as Exception handling 7
  7. 7. Exception Handling in JavaThis mechanism consists of :1. Find the problem(Hit the Exception)2. Inform that an error has occurred(Throw the Exception)3. Receive the error Information(Catch the Exception)4. Take corrective actions(Handle the Exception) 8
  8. 8. Exception Handling in JavaIn Java Exception handling is managed by 5 key words:trycatchthrowthrowsfinally 9
  9. 9. Exception Handling in JavaIn Java Exception handling is managed by 5 key words:trycatchthrowthrowsfinally 10
  10. 10. Exception Handling in Java Exception Hierarchy Package java.lang Throwable Exception errorInterruptedException RuntimeException(checked exception) (unchecked exception) 11
  11. 11. Exception Handling in Java Unchecked exception:  These exception need not be included in an method’s throws list  The compiler does not check to see if a method handles or throws these exception  these are subclasses of RuntimeException  The compiler doesnt force client programmers either to catch the exception or declare it in a throws clause.  Class Error and its subclasses also are unchecked. Checked exception:  Must be included in an method’s throws list if that method can generate one of those exceptions and does not handle it itself  These exception defined by java.lang 12
  12. 12. Exception Handling in Java Java’s unchecked RuntimeException subclasses defined in java.langException MeaningArithmeticException Arithmetic error, such as divide-by-zeroArrayIndexOutOfBoundsException Array index is out-of-boundsArrayStoreException Assignment to an array element of an incompatible typeClassCastException Invalid castEnumConstantNotPresentException An attempt is made to use an undefined enumeration valueIllegalArgumentException Illegal argument used to invoke a methodIllegalMonitorStateException Illegal monitor operation, such as waiting on an unlocked threadIllegalStateException Environment or application is in incorrect stateIllegalThreadStateException Requested operation not compatible with current thread stateIndexOutOfBoundsException Some type of index is out-of-boundsNegativeArraySizeException Array created with a negative size 13
  13. 13. Exception Handling in JavaException MeaningNullPointerException Invalid use of a null referenceNumberFormatException Invalid conversion of a string to a numeric formatSecurityException Attempt to violate securityStringIndexOutOfBoundsException Attempt to index outside the bounds of a stringTypeNotPresentException Type not fountUnsupportedOperationException An unsupported operation was encountered 14
  14. 14. Exception Handling in JavaJava’s checked Exception defined in java.langException MeaningClassNotFoundException Class not foundCloneNotSupportedException Attempt to clone an object that does not implement the Cloneable interfaceIllegalAccessException Access to a class is deniedInstantiationException Attempt to create an object of an abstract class or interfaceInterruptedException One thread has been interrupted by another threadNoSuchFieldException A requested field does not existNoSuchMethodException A requested method does not exist 15
  15. 15. Exception Handling in Javaclass Ex{ public static void main(String args[]){ int a=0; int b=2/a; }}Java.lang.ArithmeticException: / by zero at Ex.main 16
  16. 16. Exception Handling in Javatry & catch try Block Statement that causes Exception Catch Block Statement that causes Exception 17
  17. 17. Exception Handling in Java try{ Statement: } catch(Exception-type e){ statement; } 18
  18. 18. Exception Handling in Javaclass Ex{ public static void main(String args[]){ int d,a; try{ d=0; a=10/d; Once an exception is System.out.println("from try"); thrown , program control }catch(ArithmeticException e) transfers out of the try { block into the catch block.System.out.println("divsn by Zero"); Once the catch statement } is executed pgm control System.out.println("after catch"); continues with the next } line following the entire} try/catch mechanism. 19
  19. 19. Exception Handling in JavaWe can display the description of an Exception in a printlnstatement by simply passing the exception as an argument. catch(ArithmeticException ae){ System.out.println(“Exception:”+ae); } o/p Exception:java.lang.ArithmeticException: /by zero 20
  20. 20. Exception Handling in Java Common Throwable methods• getMessage(); All throwable objects can have an associated error message. Calling this message will return the message if one present.• getLocalizedMessage(); gets the localized version of error message.• printStackTrace(); sends the stack trace to the system console. This is a list of method calls that led to the exception condition. It includes line number and file names too. Printing of the stack trace is the default behavior of a runtime exception when u don’t catch it ourselves. 21
  21. 21. Exception Handling in JavaMultiple catch Statementsome cases, more than one exception could be raised bya single piece of code.such cases we can specify two or more catchclauses, each catching a different type of exception.when an exception thrown, each catch statement isinspected in order, and the first one whose type matchesthat of the exception is executed.After 1 catch statement is executed, the others arebypassed and execution continues after the try/catchblock. 22
  22. 22. Exception Handling in Javaclass Ex{public static void main(String args[]){ int d,a,len; try{ len=args.length; a=10/len; int c[]={1}; catch(ArithmeticException e){ c[10]=23; System.out.println("divsn by } Zero"+e); } catch(ArrayIndexOutOfBoundsExcept ion ae){ System.out.println("Array index"+ae); } System.out.println("after catch"); } } 23
  23. 23. Exception Handling in JavaIn multiple catch statement exception subclasses must comebefore any of their superclasses.Because a catch statement that uses a superclass will catchexception of that type plus any of its subclasses.Thus a subclass would never be reached if it came after itssuperclass.Further java compiler produces an errorunreachable code. 24
  24. 24. Exception Handling in JavafinallyIt creates a block of code that will be executed after try/catchblock has completed and before the code following try/catchblock.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 databaseThis block is optional but when included is placed after the lastcatch block of a try 25
  25. 25. Exception Handling in JavaForm:try try block{}catch(exceptiontype e){}finally finally Catch block{} finally 26
  26. 26. • If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance.• www.baabtra.com | www.massbaab.com |ww w.baabte.com
  27. 27. Contact Us

×