2. Java Exceptions
• Exception
– an event, usually some form of error, which
happens during the normal course of
program execution
• Exception Handling
– object-oriented technique to manage such
errors that comprises a group of methods
3. Java Exceptions
• Identifying exception conditions pertinent to
the application
• Locating exception handlers to respond to
possible conditions
• Monitoring when such conditions occur
4. Java Exceptions
• ArithmeticException
– caused by math errors
• ArrayIndexOutOfBoundsException
– caused by a bad array index
• ArrayStoreException
– caused when a program tries to store the wrong
type of data in an array
• FileNotFoundException
– caused by an attempt to access a nonexistent file
5. Java Exceptions
• IOException
– caused by general I/O failures
• NullPointerException
– caused by referencing a null object
• NumberFormatException
– caused when a conversion between string and
number fails
• OutOfMemoryException
– caused when there is not enough memory to
allocate a new object
6. Java Exceptions
• SecurityException
– caused by a security violation
• StackOverflowException
– caused when a program attempts to access
a nonexistent character position in a string
7. Java Exceptions
• Errors
– not exactly exceptions, but problems that
occur beyond the user or programmer’s
control
– typically ignored in program codes because
there cannot be done anything about them
8. Java Exceptions
• Checked Exceptions
– normal errors that can occur during the
execution of a program
– include problems such as array bound errors
and floating point exceptions
– usually handled by code blocks
9. Java Exceptions
• Runtime Exceptions
– probably could have been avoided by the
programmer
– ignored at the time of compilation
10. Generating Exceptions
• Method Calls
– If a method or constructor is declared to
throw an exception, then calling that
method or constructor may result in an
exception of the declared class or a subclass.
– It is a compile-time error for a method body
to generate or throw, and not catch, an
exception that is declared in the throws
clause.
12. Generating Exceptions
• Runtime Exceptions
– can occur even though the offending piece
of code does not declare that it throws such
an exception
– can be thrown by a language statement or a
method call
– NullPointerException and
ArrayIndexOutOfBoundsException
14. Generating Exceptions
• User Exceptions
– manually thrown by the programmer using
the throw statement
– takes a single argument which must be an object
that is a subclass of Throwable
throw new Exception(“Problem”);