Module 9 Exception Object Oriented ProgrammingJava Exception denotes
Module 9: Exception Object Oriented Programming(Java)
Exception denotes an abnormal event that occurs during the execution of the program and disrupts its normal flow.
Exception Class Hierarchy Object Thowable Exception Runtime Exception Error
Exception-Handling Keywords: Øtry Øcatch Øthrows Øfinally
Exception-Handling Techniques ØDeclare the exception The programmer can declare that he is aware of the exception by including the clause throws <name-of-exception>. This is the specify requirement of the Java language.
Exception-Handling Techniques ØHandle the exception This is the catch requirement of the Java language. Handle the exception by enclosing the code in a try-catch block.
Exception-Handling Techniques ØRe-throw the exception and declare this in the method signature Use the try-catch block to catch the exception and re-throw the exception to another subclass of the exception. In this event, the programmer is passing the exception up the call stock.
Exception The try / catch and finally Blocks try{ //codes that may throw exception } catch(Exception. Type e 1){ //codes that handle exception e 1 } catch(Exception. Type e 2){ //codes that handle exception e 2 } finally{ /*codes to execute whether or not errors are encountered*/ }
Exception The throw and throws Statements public void method. Name throws Exception. List { //statements throw Throwable. Instance; } Where: Exception. List – list of exception types separated by comma Throwable. Instance – an instance (object) of an exception
Exception Common Predefined Exceptions ØArithmetic. Exception – is thrown when an exceptional arithmetic condition occurred such as dividing integers by zero.
Exception Common Predefined Exceptions ØNull. Pointer. Exception – is thrown when an application attempts to use a null object that has not been initialized or instantiated.
Exception Common Predefined Exceptions ØArray. Index. Out. Of. Bounds. Exception – is thrown when an attempt is made to access an element in the array that is beyond the index of the array.
Exception User-defined Exceptions Steps on how to create and use customized exceptions: 1. Create a class that extends from Exception. 2. In a method of another class, throw a new instance of the Exception. 3. Use the method that throws exception in a trycatch block.
Question and Comments ?
- Slides: 14