Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures


Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips


Submit News
Search the site here...
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: Exception Usage

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz

Bookmark and Share

Java: Exception Usage

Common Exceptions

The most common exceptions to catch are number conversion exceptions and I/O exceptions. Here are some common exceptions to catch:
Exception Cause
NumberFormatException You tried to convert a number from an illegal String form
IOException Catch an IOException to get either of its subclasses below.
FileNotFoundException The specified file didn't exist.
EOFException Tried to read past end of file.
MalformedURLException This can be generated if you are using the java.net package.

Suggestions for catching exceptions

If you catch an exception, do something. Don't silence exceptions.
Some programs catch exceptions, then do nothing with them. This is almost always a mistake, and the underlying error should be fixed.
There are a few cases where you might want to silently ignore an exception. For example, changing the Look and Feel of the GUI may cause an exception. There's nothing to be done about it, so you don't want to stop execution. For example, I sometimes use the Liquid look and feel, and have this code.
try {
} catch (Exception e) {
    // Silently ignore -- there's nothing to be done.
Another example where exceptions are typically ignored is in calls to sleep.
try {
} catch (InterruptedException ignoredException) {
    // Silently ignore.  This thread can never cause an exception.
If you do silently ignore exceptions, enclose only one call in the try clause; do not use larger blocks of code as suggested below. Rather than silently ignoring exceptions, consider logging them to a file.
Put larger blocks of code in a try clause
Altho an exception is generated by a single statement, an entire block of code is usually affected. It is often better to put the try around the block, not just single statements.
Don't catch exceptions that you can't really do anything with
If you can't do anything useful, don't catch an exception. Let someone higher up catch it.
Exception handling is usually slow
It is generally not a good idea to use exception handling mechanism instead of simple if tests because throwing and catching exceptions is typically much slower.
Catch and rethrow exceptions to clean up
If your code has to clean up something (eg, close files, put a data structure into a consistent state, ...), it can catch exceptions, do the cleanup, and then rethrow the exception.

Printing the call stack

For debugging purposes you can print a trace of the current call stack.

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search

Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy