Learning Java/Error handling

Exceptions
All Java errors are handled as exceptions. Exceptions extend the base class, Exception. You might have seen the try and catch block used. This is a way to "catch" exceptions - this essentially allowing you to handle various unexpected errors gracefully.

Life isn't perfect. Much as we'd like to think otherwise, sometimes we encounter exceptional behavior. Things like missing files, badly formatted data to be parsed, and operations on  things are just a few of the many exceptions you may meet in your programming career. When these things happen, the JVM throws a temper tantrum, or (more technically) throws an exception.

How do you know if an exception could be thrown? The hard way is when the compiler complains that you didn't properly handle the method. The easy way involves the API documentation. The standard API documentation on the Java site begins a method's documentation with its header: the access level, return type, method name, parameters, miscellaneous modifiers, and the declared exceptions. For example, the  method in class   throws. Its header looks like this:

If a method might throw an exception, it has to declare that exception in its header using the  statement. (Note: just because a method declares an exception doesn't mean that it will throw it, just that it might.) If you use a risky method, you have to tell the compiler what to do in case something bad happens. This is called catching the exception.

Try and Catch
Try and catch blocks are really simple. The basic syntax follows. For this example, we'll try to read a file that doesn't exist. (The details of file I/O comes later in this tutorial; the important part is just that this code is risky: if the file "myfile.txt" doesn't exist, the  constructor throws a  .) Whenever a method of some class could throw an exception, you should use the try/catch block. The compiler tries to run the code in the  block. If any statement in the block throws an exception, the JVM jumps to the appropriate  block; that is, the   block that names the thrown exception. If no code in the  block throws an exception, the   block is skipped.

Consider the code above. If the named file exists, the  block runs to completion, and the   block is skipped. The output is

Ready? Go! I think I can... I knew I could! All done.

If the file does not exist, however, the  block halts at the   constructor, and the   block is run:

Ready? Go! I think I can... File not found. All done.

Exercise 1: Make a program that asks for an integer in a JOptionPane Input Message (you can use JOptionPane.showInputDialog(null, "TEXTHERE");). Then, it formats it into an integer and prints the number one above it. If the integer is not valid, it says "Bad Integer." Name your class StrToInt.

Answer:

Finally Blocks
This is a continuation of try/catch blocks. The finally statement is always executed. It is the place where you can tidy things up, rather than duplicating code. However, you should be aware when it is executed in the flow of control.

First of all, here is where you put it: Now, when it executes: it occurs before the catch statement (if the catch statement happens) or after the try statement. Essentially it happens here and here: The (arguably) best part about the finally block is that it can execute code "after" returning in a method. As a very basic (and bad, but working) example, this means you could have: Simple, eh?