Unformatted text preview:

Exceptions in Java3-10-2005Opening DiscussionDo you have any questions about the quiz?What did we talk about last class? Do you have any code to show?Do you have any questions about the assignment?When you have a function in which something goes wrong, how does it tell the rest of the program? Do you always put in code to make sure the functions you call execute properly?Midterm ResultsThe median grade on the exams was an 89. I think I graded too lightly. :)A B C D F00.511.522.533.544.555.566.57Error HandlingIn the code that you are probably used to, there are two ways that a function can tell you if an error occurred.Return an error code.Set a flag that should be checked.Both of these are very easy to ignore which can lets the errors “propagate” and makes debugging much more difficult.Enter ExceptionsAn alternate approach to error handling is the use of exceptions. As their name implies, exceptions are things used for exceptional events.When an error occurs, the code will “throw” an exception. When an exception is thrown control pops up the stack until code is found to handle it. If no handler is found that thread exits completely.try, catch, and finally BlocksThere are 4 syntactic components of dealing with exceptions. The ones you will write most are try and catch blocks.When you have a section of code that can have an exception thrown in it that you want to handle, you put it in a try block.After the try block you can have one or more catch blocks. Each one specifies a type and catches anything of that type (including subtypes).A finally block is like a default. In addition, it ALWAYS happens.throws and throwSometimes a method can have an exception occur in it, but it doesn’t know how to handle it. In this situation, the exception should be added to the throws clause of the method declaration.If you want to throw your own exceptions you use the throw statement. It is followed by an object that is the exception to be thrown.Checked vs. Unchecked ExceptionsThere are two broad categories of exceptions.Checked exceptions must either be caught, or they must appear in the throws clause of the method.Unchecked exceptions don’t have to do this and often shouldn’t be handled. If an unchecked exception arises it typically signifies a major problem and the code should crash.Subtypes of RuntimeException are unchecked.Benefits of ExceptionsExceptions are nice because they can’t be ignored. They tell you there is a problem immediately instead of letting the code run on until it has a serious problem or just leaving logic errors.The Exception class also has handy methods like printStackTrace() that can be used to help with the debugging process as well.CodeLet’s go look in the javadocs at some of the function calls that can throw exceptions then write some code that deals with those exceptions.We can also go put in exceptions in our code that we have written.Minute EssayThe line new FileInputStream(fileName) can throw two exceptions. Look those up in the javadocs, and write code that would catch them and print out semi-meaningful messages.Enjoy your Spring Break. The design for assignment #5 is due on the Tuesday when you get


View Full Document

TRINITY CSCI 1321 - Exceptions in Java

Documents in this Course
Recursion

Recursion

11 pages

Iterators

Iterators

10 pages

Actors

Actors

9 pages

Recursion

Recursion

15 pages

Recursion

Recursion

10 pages

Threads

Threads

7 pages

Trees

Trees

11 pages

Load more
Download Exceptions in Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exceptions in Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exceptions in Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?