Java Exceptions handling Question

S

Shingitty Bunky

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
 
V

VisionSet

Shingitty Bunky said:
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.
 
E

Eric Sosman

Shingitty said:
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 }
 
A

Andrew Thompson

Shingitty Bunky said:
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.. ;-)
 
C

Chris Smith

Eric said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top