Exceptions in Java3-10-2005Opening DiscussionDo 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 ResultsThe 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 HandlingIn 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 ExceptionsAn 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 BlocksThere 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 throwSometimes 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 ExceptionsThere 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 ExceptionsExceptions 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.CodeLet’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 EssayThe 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