Show Menu
Cheatography

Java Exceptions (OCA) Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

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 inheri­tance

java.l­ang.Object
super
java.l­ang.Th­rowable
sub class of Object
java.l­ang.Ex­ception
sub class of Throwable
java.l­ang.Ru­nti­meE­xce­ption
sub class of Exception
java.l­ang.Error
sub class of Throwable

Checked exceptions

must be handled or declared /throws by programmer
must in try ... catch block, or throws explicitly
IOExce­ption
Fileot­Fou­ndE­xce­ption, SQL, URI related exception
CloneN­otS­upp­ort­edE­xeption
all java.l­ang.Ex­ception and its subclass are checked exception, exception java.l­ang.Ru­nti­meE­xce­ption and its subclass.

Unchecked exception

java.l­ang.Ru­nti­meE­xce­ption or its subclass
could handled or throws by progra­mmer, but not required.
Common unchecked /runtime exceptions
1 Arithm­ati­cEx­ception
divided by 0
2 ArrayI­nde­xOu­tOf­Bou­nds­Exc­eption
IndexO­utO­fBo­und­sEx­ception
String­Ind­exO­utO­fBo­und­sEx­ception
3 ClassC­ast­Exc­eption
parent­-child relation
4 Ilegal­Arg­ume­ntE­xce­pti­on(app)
Number­For­mat­Exc­ept­ion(app throw)
5 NullPo­int­Exc­eption
6 Illega­lSt­ate­Exc­eption
JVM env
7 Securi­tyE­xce­ption
awt
Most exception are checked due to extends Exception, not Runtim­eEx­ception
Compile but throw exception at runtime
 

Java Error

Should not be handled by progra­mmer, java will terminate the program
ClassI­nni­tia­liz­erError
Error in Static initia­liz­ation of class
StackO­vef­low­Error
memory stack space used
NoClas­sFo­und­Error
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 declar­ation statement
throw new Except­ion­("Error Messag­e");
nested­/ch­ained excpetions
Catch and/or finally{} throws exceptions
exception mask
nested except­ions, the last throw win, prior throw masked
 
put try..catch in finall{} to avoid exception mask
calling method w/ throws
handled or throws again;­req­uired 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..s­uper, 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