1 / 12

Exception Handling

Exception Handling . An exception is a run time error that can be handled in the application itself and doesn't result in abnormal program termination.

joben
Download Presentation

Exception Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception Handling An exception is a run time error that can be handled in the application itself and doesn't result in abnormal program termination. In java, each run time error is represented by object of a class ,for this a class hierarchy is define is define & the root of this class hierarchy is an abstract class name Throwable that represents basics features of a run time error. It has two concrete subclasses Exception and Error. Throwable Exception Error

  2. Exception(super class of those run time errors that can be handled by exception handling mechanism ) • Error(super class of those runtime errors that can’t be handled by programmatically) • Commonly used subclasses of exception : • ArithmeticException represents invalid arithmetic output such as division by zero. • ArrayIndexOutOfBoundsException represents an attempts to access non existent array element. Ex command line arguments .

  3. NullPiointerException represents an attempt to use null containing reference variable to refer data members or method of an object.Ex: • Public Static void fun(A x) • { • x.display(); • } • //A a =new A(); • //fun(a); • //fun(null);

  4. NumberFormateException: represents an attempt to convert a non numeric string into a number . • Interger.parseInt(“AB”); • IOException: represents an invalid I/O operation . • Commonly used Subclasses of Error: • VirtualMachineError • StackOverFlowError • NoClassDefondError • NoSuchMethodError • etc.

  5. Exception Handling Mechanism • E.H.M. provides structed way to handled runtime errors in a program. This is facilitated with the help of following 5 key words : • 1)try 2)catch 3) throw 4) throws 5) finally. • Try: try keyword represents monitoring block I .e. it is used to execute statement may result in a run time error. • Syntax: • Try • { • Statements that may result in a run time error • }catch(Exception e) • { • Statements only to be executed in case of exception • }

  6. Try • { • Statements that may result in an exception • } • Finally • { • Statements that shall be executed whether an exceptions occurs or not . • }each try block as associated with it at least one catch or finally block or both.

  7. Catch block A catch keyword represents an execution handled block .it contains statements that are executed only if an exception is called following sequence of events occurs when runtime error is generated. • JVM creates an object of exception class to represents the run time error. • Exception object is thrown i.e. control is transferred from the try block to catch block. • Exception Object is called in a catch block and statements of catch block are executed. Note: Execution of program is resume from the statement s next to the catch block.

  8. Note: if a try block has associated with it a specific and generalized Exception handler then generalized handler then generalized exception handler must be the last one . Throw: it is used for explicit exception throwing . • Syntax: throw(Exception obj); • Usages of throw keyword: • It is used in the following cases • 1) To customize the message displayed in case of exception . • 2) To throw user defined exception . • 3) To rethrow an exception that is called in a catch block.

  9. All Exception classes provides following constructors • Public Exception (); • Public exception(String msg); • Note: By creating the exception object using the second constructor message can be customized. • Throws : it is used to specify the exceptions that can be thrown by a method . Syntax: access modifier return type method Name(arg type )throws exception1……..; EX: public static int parseInt(String no)throws NumberFormateException;

  10. Difference b/w throw & throws • 1) throws keyword represents that specified exception can be thrown by method where as throw keyword is used for explicitly throwing exceptions. • Throws keyword is part of the signature whereas throw keyword is part of the implementation. . • Exceptions are divided into two categories : 1)checked 2) unchecked • Checked exceptions: are those exceptions that are force by the compiler either to be properly caught declared to be thrown in the method in which they are generated. 2) Unchecked Exceptions: are those exceptions that are not force by the compiler either properly caught or to be declared with throws keyword. Unchecked Exception either represents those runtime errors that can not be handled using exception handling mechanism or frequently occurred run time error.

  11. Object of type error and its subclasses ,runtime exception and its subclass are unchecked exception ,all other are checked exception. • throwable • Error(unchecked) Eception • RuntimeException • ArithmeticException • ArrayIndexOutOfBoundsException • NullPointerException • NumberFormateException • (All are unchecked) • IoException(checked) • TnterruptedException(checked)

  12. Finally keyword • It is used to define a block of statement that is executed 100% certainty whether an exception occurs or not . • Note : if finally block contains a branching instruction then branching instruction of the finally nullifies other branching instruction executed in the method before finally.

More Related