Java Exceptions handling Question

Discussion in 'Java' started by Shingitty Bunky, Dec 16, 2003.

  1. Hi,

    Is there a catch all exception handling mechanism in Java? I was not
    able to find an equivalent statement such as catch(...) in Java as you
    can in C++.

    Thanks for any help with this.

    SB
    Shingitty Bunky, Dec 16, 2003
    #1
    1. Advertising

  2. Shingitty Bunky

    VisionSet Guest

    "Shingitty Bunky" <> wrote in message
    news:...
    > Hi,
    >
    > Is there a catch all exception handling mechanism in Java? I was not
    > able to find an equivalent statement such as catch(...) in Java as you
    > can in C++.
    >
    > Thanks for any help with this.
    >


    Yes, if an Exception is thrown then this will catch it

    try {
    // do Stuff that may throw an Exception of any kind...
    }
    catch(Exception e) {
    // ...it will always be caught
    // This will catch checked and runtime exceptions
    // because all exceptions are sub class of Exception
    }

    If you want to catch errors as well, not that you should,
    then catch(Throwable t) will do this, because all exceptions and errors
    subclass Throwable.

    --
    Mike W
    VisionSet, Dec 16, 2003
    #2
    1. Advertising

  3. Shingitty Bunky

    Eric Sosman Guest

    Shingitty Bunky wrote:
    >
    > Hi,
    >
    > Is there a catch all exception handling mechanism in Java? I was not
    > able to find an equivalent statement such as catch(...) in Java as you
    > can in C++.


    try { something } catch (Throwable t) { whatever }

    Note that this will catch a lot of things few sane
    programs would want to catch. A more likely formula is

    try { something } catch (Exception e) { whatever }

    --
    Eric Sosman, Dec 16, 2003
    #3
  4. "Shingitty Bunky" <> wrote in message
    news:...
    > Hi,
    >
    > Is there a catch all exception handling mechanism in Java? I was not
    > able to find an equivalent statement such as catch(...) in Java as you
    > can in C++.


    a 'catch' will catch any instance of that exception,
    or any _subclass_ of it, so if you had a missing file
    and the following code..

    catch(FileNotFoundException fnfe) { fnfe.prntStackTrace(); }
    catch(Exception e) { fnfe.prntStackTrace(); }

    The exception would be caught by the first catch.

    Now, if you reverse those statements,..

    catch(Exception e) { fnfe.prntStackTrace(); }
    catch(FileNotFoundException fnfe) { fnfe.prntStackTrace(); }

    ...the fnfe cannot even be reached, because the
    catch(Exception e).. would intercept the exception.
    That is, if it compiled - I expect it would throw
    an 'unreachable code' type exception.

    Though that is left as an exercise for the OP
    to confirm.. ;-)

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
    Andrew Thompson, Dec 16, 2003
    #4
  5. Shingitty Bunky

    Chris Smith Guest

    Eric Sosman wrote:
    > Shingitty Bunky wrote:
    > >
    > > Hi,
    > >
    > > Is there a catch all exception handling mechanism in Java? I was not
    > > able to find an equivalent statement such as catch(...) in Java as you
    > > can in C++.

    >
    > try { something } catch (Throwable t) { whatever }
    >
    > Note that this will catch a lot of things few sane
    > programs would want to catch. A more likely formula is
    >
    > try { something } catch (Exception e) { whatever }


    To add to Eric's reply, please be careful about doing this. The
    exception handling mechanism is specifically designed to let you do
    something about specific exceptions that you are anticipating, and pass
    by other exceptions that you aren't. It's goo to use that facility as
    designed. There's a huge temptation, when catching a superclass like
    Exception or Throwable, to *assume* that the exception you just caught
    is one that you were anticipating, but that may not be the case.
    Remember that Exception includes things like NullPointerException or
    ArrayIndexOutOfBoundsException, which are often entirely unexpected;
    Throwable includes things like InternalError, which are even less
    expected and even more dangerous if you try to go on without
    understanding why they occurred.

    In other words, your program crashing is *not* the worst thing that
    could happen. The worst thing that could happen is for your program to
    break and you to not find out about it because you put in overly-zealous
    error handling.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Dec 16, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ahmed Moustafa
    Replies:
    5
    Views:
    29,992
    Chris Smith
    Jul 14, 2004
  2. Paul Miller
    Replies:
    3
    Views:
    1,009
    Alex Martelli
    Nov 12, 2003
  3. Replies:
    3
    Views:
    599
    Sherm Pendley
    Apr 16, 2007
  4. Lie
    Replies:
    3
    Views:
    601
  5. Rolando Abarca
    Replies:
    0
    Views:
    121
    Rolando Abarca
    Jun 3, 2009
Loading...

Share This Page