inheritance
Yeabsira
Exception
Exception
Handling
Data abstraction and Inheritance
Yeabsira
Addis Ababa Institute of Technology
[email protected] 19-Nov-2024
inheritance
Yeabsira
Exception
Exception
Handling
Exception
Exception Handling
inheritance
Exception (Introduction)
Yeabsira
Exception
▶ An exception is an indication of a problem that occurs Exception
Handling
occurs during a program’s execution indicating its
failure.
▶ In Exception handling exceptions are resolved and the
program can be made to run without halt inspite of the
exception
▶ common exceptions are
* Performing illegal arithmetic => int a= 4/0;
//java.lang.ArithmeticException: / by zero
* Accessing out-of-bound elements =>int b[]= {1,2};
b[2];//java.lang.ArrayIndexOutOfBoundsException: 2
* illegal arguments to methods => fun(1) for function
public void fun(String a)
* Hardware failures
* writing to a read-only file
inheritance
Exception: Stack trace
Yeabsira
Exception
Exception
Exception message format Handling
Exception class: additional info on the exception
at class.method(file:line number)
Example
public class myClass{
public static void main(String[] args){
int a=4/0;
}
}
------
Exception in thread ”main”
java.lang.ArithmeticException: / by zero
at myClass.main(myClass.java:3)
inheritance
try-catch
Yeabsira
Exception
try{ Exception
Handling
// exception causing code
}
catch(Exception-Type ex){
// what to do when the exception is thrown
}
Example
try{
int a=4/0;
}
catch(ArithmeticException ex){
System.out.println(”You can not divide by Zero”);
}
inheritance
checked and unchecked exceptions
Yeabsira
Exception
Unchecked Exception checked Exception
Exception
Usually occur because of pro- Usually occur because of er- Handling
gramming errors rors programmer cannot con-
trol
They are numerous and can They are less frequent and
be ignored by the programmer they cannot be ignored by the
programmer
subclass of RuntimeException not a subclass of RuntimeEx-
ception
▶ NullPointerException ▶ IOException
▶ ▶ FileNotFoundException,
IllegalArgumentException ▶ SocketException
▶ IllegalStateException
inheritance
checked exceptions handling
Yeabsira
try-catch Exception
Exception
void readFile(){ Handling
try{
FileReader reader = new FileReader (”file.txt”);
// read from file . . .
} catch (FileNotFoundException e){
throws e; }
}
- OR -
Duck the exception
void readFile() throws FileNotFoundException {
// other methods calling on this method should
implement the try catch not this one
FileReader reader = new FileReader(”file.txt”);
// read from file . . .
}
inheritance
multiple exceptions
Yeabsira
Exception
Exception
Handling
try with multiple catch
void readFile(){
try{
FileReader reader = new FileReader (”file.txt”);
// read from file . . .
} catch (FileNotFoundException ex1){
throws ex1;
}
catch (IOException ex2){
throws ex2;
}
}
inheritance
Finally Block
Yeabsira
Finally block Exception
Exception
▶ The finally block will execute whether or not an Handling
exception is thrown
▶ The finally block will execute even if a try block exits by
using a return, break or continue statement
void readFile(){
try{
FileReader reader = new FileReader (”file.txt”);
// read from file . . .
} catch (FileNotFoundException ex1){
System.out.println(”There was an exception”);
}
finaly {
System.out.println(”This will almost always be
executed”); }
}
inheritance
Exception hierarchy
Yeabsira
Exception
Exception
Handling