exceptions Flashcards
run-time errors
> Illegal/impossible operations to execute
Detected during program execution
But not detectable at compile time
Treated asexceptionsin Java
(examples: division by zero, using null reference, illegal format conversion)
what to do when an error occurs…
alternatives
> Ignore error
Print error message and terminate
Have the method handle the error in the code where the problem lies as best as you can
Have the method pass it off to someone else to handle
Usually an error code is returned to whoever called the method
> Modern language approach:Exception!
(note: in language like C, a “core dump” takes place)
how to indicate when a program generates an exception
“a program throws an exception”
In Java an exception is represented by an object
what to do when an exception occurs…
do nothing:
>Usually your program will be terminated by the JVM
>Stack trace is printed showing where exception was generated (Red and blue in Eclipse window)
do something:
>In some applications aborting the program is not an option (Email processor, web/database server)
> We can “handle” the exception by “catching the exception” and defining code to be executed
try
catch
Use try { } to enclose code that can potentially throw an exception
Use catch(EXCEPTIONTYPE e) { } to “catch” the exception and define the code to execute when the exception occurs
why is try catch somewhat similar to if else
> when an exception occurs control “jumps” to the catch clause and code after the point where the exception occurred is not executed
If the exception is not thrown the code of the catch clause is ignored
exception object
When an exception is thrown, a new exception object is created, which encodes information about the nature of the exception
Exception(String message)
Constructor taking an explanation as an argument
String getMessage()
Returns a message describing the exception
void printStackTrace()
Prints the contents of the Java call stack at the time of the exception
ArithmeticException
divide by zero
NullPointerException
attempt to access an object with a null reference
IndexOutOfBoundsException
array or string index out of range
ArrayStoreException
attempting to store an object of type X in an array of type Y
EmptyStackException
attempt to pop an empty Stack (java.util)
IOException
attempt to perform an illegal input/output operation (java.io)
NumberFormatException
attempt to convert an invalid string into a number (e.g., when calling Integer.parseInt( ) )
Exception
The most generic type of exception. All of these are Objects in Java’s class library (most in java.lang) Every Exception object supports a group of methods (these methods are defined in this stack of flashcards)
exception propagation
(looks for exception handlers)
Java pops back up the call stack to each of the calling methods to see whether the exception is being handled in a catch block of the method
what happens when your code does not catch your exceptions
java aborts your program
try
finally
similar to try catch, however finally always runs (whether there is an error or not)
(For example if the return occurs before the finally clause, the clause will be executed and then we will return)
write your own exceptions…
> You can throw exceptions using throw
>Never leave the catch clause empty (when in doubt call printStackTrace)