What's exception
Things going wrong? |
java throwable object created and could be caught |
Message, stack trace |
java 7: catch >1 exception in one catch block |
Three types of Exceptions
checked |
JVM: hi, something wrong, you should check or declare before I allow it compiled |
unchecked |
JVM: never mind, I could handle it |
Error |
JVM: don't try to handle it, something really really nasty happened, I will end it |
Exception class inheritance
java.lang.Object |
super |
java.lang.Throwable |
sub class of Object |
java.lang.Exception |
sub class of Throwable |
java.lang.RuntimeException |
sub class of Exception |
java.lang.Error |
sub class of Throwable |
Checked exceptions
must be handled or declared /throws by programmer |
must in try ... catch block, or throws explicitly |
IOException |
FileotFoundException, SQL, URI related exception |
CloneNotSupportedExeption |
all java.lang.Exception and its subclass are checked exception, exception java.lang.RuntimeException and its subclass.
Unchecked exception
java.lang.RuntimeException or its subclass |
could handled or throws by programmer, but not required. |
Common unchecked /runtime exceptions |
1 ArithmaticException |
divided by 0 |
2 ArrayIndexOutOfBoundsException |
IndexOutOfBoundsException |
StringIndexOutOfBoundsException |
3 ClassCastException |
parent-child relation |
4 IlegalArgumentException(app) |
NumberFormatException(app throw) |
5 NullPointException |
6 IllegalStateException |
JVM env |
7 SecurityException |
awt |
Most exception are checked due to extends Exception, not RuntimeException |
Compile but throw exception at runtime
|
|
Java Error
Should not be handled by programmer, java will terminate the program |
ClassInnitializerError |
Error in Static initialization of class |
StackOveflowError |
memory stack space used |
NoClassFoundError |
class file not found in classpath |
Syntax
try {} |
{} required even only one statement |
catch (){} |
at lease one catch or finally block required |
muliple catch(){} |
ordered by sub -super, or else compiler shout: never run code block found |
|
at most 1 catch blocks executed |
finally {} |
always executed, exception System.exit() called before it |
return will not prevent finally block run
Throwing an exception
throws in declaration statement |
throw new Exception("Error Message"); |
nested/chained excpetions |
Catch and/or finally{} throws exceptions |
exception mask |
nested exceptions, the last throw win, prior throw masked |
|
put try..catch in finall{} to avoid exception mask |
calling method w/ throws |
handled or throws again;required for checked exception, optional for unchecked |
subclass exception |
subclass of parent class exception throwed |
Catch an exceptions
multiple catch block |
at most one executed |
|
order sub..super, or else Never run block found error |
|
the first matched block win |
catch block may not exist |
|
|
Print exception message
//print exception type and message
System.out.println( e);
//print message
System.out.println( e.getMessage());
//print stack trace
e.printStackTrace();
//e.getCause()
//e.getStackTrace()
|
do not swallow exception, should always decide what to do next at exception
|