Basic Java 4
Basic Java 4
Objectives
Define Exceptions
Use try, catch, and finally statements
Describe exception categories
Identify common exceptions
Develop programs to handle your own
exceptions
Exceptions
try{
//code that might throw a particular exception
}catch(MyException myExcept){
//code to execute if a MyExceptiontype is thrown
}
catch(Exception otherExcept){
//Code to execute if a general Exception is
//thrown
}
Call Stack Mechanism
try{
startFaucet();
waterLawn();
}catch(BrokenPipeException e){
logProblem(e);
}finally{
stopFaucet();
}
Exception Categories
T h ro w a b le
E rro r E x c e p tio n
S ta c k O v e rflo w E rro r O u tO fM e m o ry E rro r A rith m e tic E x c e p tio n N u llP o in te rE x c e p tio n In d e x O u tO fB o u n d s E x c e p tio n E O F E x c e p tio n F ile N o tF o u n d E x c e p tio n
Common Exceptions
ArithmeticException
NullPointerException
NegativeArraySizeException
ArrayIndexOutOfBoundsException
SecurityException
The Handle or Declare Rule