Exceptions
Chapter 15
15
Throwing and Catching Exceptions
When a program runs into a problem
that it cannot handle, it throws an
exception.
Exce...
15
Unhandled Exceptions
When Java encounters an unhandled
exception (e.g., divide by zero), the compiler
provides informat...
15
When an Exception is Thrown
Processing stops at point of exception.
Java Virtual Machine (JVM) looks for a
catch block ...
15
Catching Exceptions
A try / catch block is the general
approach to exception handling.
The try block contains the code ...
15
Throwing Exceptions
Use the throw keyword when throwing
the exception.
if ( divisor == 0 )
{
throw new ArithmeticExcept...
15
Exception Classifications
All exceptions ultimately derive from
Throwable class, which is divided
into Error and Except...
15
Exception Class
The Exception class is divided into
IOException and RunTimeException.
IOException problems are typicall...
15
CustomExceptions
Creating your own exception classes
allows you to throw more specific
exceptions.
Exceptions can be us...
15
The throws Keyword
A method can declare that it throws a
certain type of exception using the
throws keyword.
“I am the ...
15
The finally Keyword
The finally keyword works in
conjunction with a try / catch block.
finally “cleans up” after an exc...
Upcoming SlideShare
Loading in …5
×

Chapter 15

217 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
217
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter 15

  1. 1. Exceptions Chapter 15
  2. 2. 15 Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions are either caught (handled) or unhandled. Unhandled exceptions cause the application to exit.
  3. 3. 15 Unhandled Exceptions When Java encounters an unhandled exception (e.g., divide by zero), the compiler provides information that can help you find the source of the error.
  4. 4. 15 When an Exception is Thrown Processing stops at point of exception. Java Virtual Machine (JVM) looks for a catch block to handle the exception. JVM unwinds the stack, or retraces its steps searching for a catch block. If no catch block is found, the program terminates with the default handler.
  5. 5. 15 Catching Exceptions A try / catch block is the general approach to exception handling. The try block contains the code that might throw an exception. The catch block catches and handles the exception, if thrown.
  6. 6. 15 Throwing Exceptions Use the throw keyword when throwing the exception. if ( divisor == 0 ) { throw new ArithmeticException(); }
  7. 7. 15 Exception Classifications All exceptions ultimately derive from Throwable class, which is divided into Error and Exception subclasses Error exceptions are thrown by Java system internal errors (e.g., “out of memory”). Error exceptions are never thrown by Java programmers.
  8. 8. 15 Exception Class The Exception class is divided into IOException and RunTimeException. IOException problems are typically beyond your control (e.g., the disk is full). RunTimeException usually indicates a programmer error (e.g., trying to write past the end of an array).
  9. 9. 15 CustomExceptions Creating your own exception classes allows you to throw more specific exceptions. Exceptions can be used polymorphically. Multiple exception handling must be coded in order from specific to more general. An exception class derived from a more general exception must be caught first.
  10. 10. 15 The throws Keyword A method can declare that it throws a certain type of exception using the throws keyword. “I am the readFile method and I might throw an IOException.” public void readFile(String fileName) throws IOException
  11. 11. 15 The finally Keyword The finally keyword works in conjunction with a try / catch block. finally “cleans up” after an exception Important to include finally block to limit the chance of resource leaks due to open connections or files

×