java programming exceptions handling. topics: learn about exceptions try code and catch exceptions...
TRANSCRIPT
![Page 1: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/1.jpg)
Java Programming
Exceptions Handling
![Page 2: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/2.jpg)
Topics:
Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch multiple Exceptions Use the finally block Understand the limitations of traditional
error handling
![Page 3: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/3.jpg)
Specify the Exceptions a method can throw Handle Exceptions uniquely with each catch Trace Exceptions through the call stack Create your own Exceptions
![Page 4: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/4.jpg)
Learning about Exceptions
Exception- An unexpected or error conditionFor example:
You issue a command to read a file from a disk, but the file does not exist
You write data to a disk but the disk is full or unformatted
The program attempts to divide by zero
![Page 5: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/5.jpg)
Learning about Exceptions
Exception Handling- The object-oriented techniques to manage such errors comprise the group of methods know as exception handlingThere are two basic classes of errors
Error and Exception
Both classes descend from the Throwable classYou must expect the unexpected
![Page 6: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/6.jpg)
Throwable
Exceptions(Checked/unchecked)
AWTError ThreadDeath
Error(Unchecked)
RuntimeException(Unchecked)
IOException(Checked)
Exception Class Hierarchy
OutOfMemoryChecked Exception: “must catch or declare”
![Page 7: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/7.jpg)
Learning about Exceptions
Error class- Represents more serious errors from which your program usually cannot recoverFor example, spelling a class name incorrectly
or storing a required class in the wrong folder
![Page 8: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/8.jpg)
Learning about Exceptions
Exception class- Comprise less serious errors that represent unusual conditions that arise while a program is running, and from which the program can recoverExamples of Exception class errors include
using an invalid array subscript or performing certain illegal arithmetic operations
![Page 9: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/9.jpg)
Trying Code and Catching Exceptions try block- A block of code you attempt to
execute, while acknowledging that an Exception might occur
Consists of the following elements:The keyword tryAn opening curly braceStatements that might cause exceptionsA closing curly brace
![Page 10: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/10.jpg)
Trying Code and Catching Exceptions catch block- Must code at least one catch
block immediately following a try blockA segment of code that can handle an
Exception that might be thrown by the try block that precedes it
![Page 11: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/11.jpg)
Creating a catch block
Create a catch block by typing the following elements: The keyword catch An opening parenthesis An Exception type A name for an instance of the Exception type A closing parenthesis An opening curly brace Statements that take the action you want to use to
handle the error condition A closing curly brace
![Page 12: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/12.jpg)
General format of a try…catch pair
![Page 13: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/13.jpg)
Using the Exception GetMessage() Method getMessage() method- To retrieve Java's
message about any Throwable Exception named someExceptionCode someException.getMessage()
![Page 14: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/14.jpg)
Throwing and Catching Multiple Exceptions
Can place as many statements as you need within a try block
Can catch as many Exceptions as you want If you try more than one statement, only the
first error-generating statement throws an Exception
As soon as the Exception occurs, the logic transfers to the catch block
![Page 15: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/15.jpg)
Throwing and Catching Multiple Exceptions
Multiple catch blocks are examined in sequence until a match is found for the type of Exception that occurred
The match catch block executes and each remaining catch block is bypassed
![Page 16: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/16.jpg)
![Page 17: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/17.jpg)
![Page 18: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/18.jpg)
Using the finally Block
The finally block contains actions you must perform at the end of a try…catch sequence
Code within a finally block executes whether or not the preceding try block identifies exceptions
The finally block performs clean-up tasks that must happen
![Page 19: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/19.jpg)
![Page 20: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/20.jpg)
Understanding the Limitations of Traditional Error Handling Before object-oriented programming,
potential errors were handled using error-prone methods
Object-oriented exception handling allows you to isolate error-handling code
![Page 21: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/21.jpg)
![Page 22: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/22.jpg)
![Page 23: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/23.jpg)
Specifying the Exceptions a Method can Throw When you write a method that might throw an
Exception, you can type the clause throws <name> Exception after the method header to indicate the type of exception that might be thrown
When you use any method you must know The method’s name The method’s return type The type and number of arguments it requires The type and number of Exceptions the method throws
![Page 24: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/24.jpg)
Specifying the Exceptions a Method can Throw Method's signature- A method's header,
including its name, any arguments, and any throws clause
![Page 25: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/25.jpg)
Handling Exceptions Uniquely with Each catch When you use a class and one of its
methods throws an Exception, the class that throws the Exception does not have to catch it
Your calling program can catch the Exception, and then you can decide what to do with it
![Page 26: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/26.jpg)
Tracing Exceptions Through the Call Stack Call stack- Where the computer stores the list of
locations to which the system must return When a method throws an Exception, and if the
method does not catch the Exception, the Exception is thrown to the next method up the call stack
You can display this list using the printStackTrace() method
![Page 27: Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch](https://reader031.vdocuments.net/reader031/viewer/2022032312/56649e315503460f94b21ab5/html5/thumbnails/27.jpg)
Creating Your Own Exceptions
Java provides over 40 categories of Exceptions that you can throw in your programs
Java also allows you to create your own Exceptions
You should extend your Exceptions from the Exception class
When you create an Exception, it's conventional to end its name with Exception