[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
JavaFAQ_Exceptions
What are exceptions?
Dealing with exceptions thrown by the interpreter
What are exceptions?
An exception is Java's method of handling errors that occur while running the program.
This relatively simple chunk of code looks for a file named readme.txt, and opens it without reading it or doing anything once it is opened. The important thing is that if readme.txt doesn't exist, calling the constructor method FileInputStream() will send an exception named FileNotFoundException, strangely enough.
So how do we deal with pesky exceptions? One way to prevent exceptions from prematurely ending a program is to put them in a try..catch block:
What the try..catch block does is attempt to do whatever is in the try block--in this case, opening a file. If the file doesn't exist, it will throw the FileNotFoundException. This time, control will move to the catch block, where the exception has been "caught," and Java will do whatever's in the block--in this case, print the words "An exception has occured:", followed by the message embedded in the exception. (As you might have noticed, exceptions, like many things in Java, are objects, and as such have their own methods.)
Analyzing exceptions
There are two ways to deal with exceptions:
If you choose to throw the exception, you simply pass it on. Exceptions start in the method where they originated and "bubble up." For example:
If it turns out the file readme.txt doesn't exist, FileInputStream() will send an exception to grandchild(), who will send it to child(), who will send it to parent(). At some point, of course, the exception has to be caught; where this is is up to you. To have methods throw exceptions, add the words throws (name of exception) at the end of the line declaring the method. Any exception that isn't in a try..catch block will be bubbled up.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
JavaFAQ_Exceptions
Exceptions
ContentsWhat are exceptions?
Dealing with exceptions thrown by the interpreter
What are exceptions?
An exception is Java's method of handling errors that occur while running the program.
FileInputStream fileopener = new FileInputStream("readme.txt");
This relatively simple chunk of code looks for a file named readme.txt, and opens it without reading it or doing anything once it is opened. The important thing is that if readme.txt doesn't exist, calling the constructor method FileInputStream() will send an exception named FileNotFoundException, strangely enough.
So how do we deal with pesky exceptions? One way to prevent exceptions from prematurely ending a program is to put them in a try..catch block:
try {
FileInputStream fileopener = new FileInputStream("readme.txt");
}
catch(FileNotFoundException ex) {
System.out.println("An exception has occured:\n" + ex.getMessage());
}
What the try..catch block does is attempt to do whatever is in the try block--in this case, opening a file. If the file doesn't exist, it will throw the FileNotFoundException. This time, control will move to the catch block, where the exception has been "caught," and Java will do whatever's in the block--in this case, print the words "An exception has occured:", followed by the message embedded in the exception. (As you might have noticed, exceptions, like many things in Java, are objects, and as such have their own methods.)
Analyzing exceptions
There are two ways to deal with exceptions:
- Handle the exception right where it occurs
- Throw the exception on to the next method
If you choose to throw the exception, you simply pass it on. Exceptions start in the method where they originated and "bubble up." For example:
public static void parent() {
child();
}
public static void child throws FileNotFoundException() {
grandchild();
}
public static void grandchild throws FileNotFoundException() {
FileInputStream fileopener = new FileInputStream("readme.txt");
}
If it turns out the file readme.txt doesn't exist, FileInputStream() will send an exception to grandchild(), who will send it to child(), who will send it to parent(). At some point, of course, the exception has to be caught; where this is is up to you. To have methods throw exceptions, add the words throws (name of exception) at the end of the line declaring the method. Any exception that isn't in a try..catch block will be bubbled up.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
