aspose file tools*
The moose likes Java in General and the fly likes I can't create my custom exceptions!? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I can Watch "I can New topic
Author

I can't create my custom exceptions!?

Jack Kay
Ranch Hand

Joined: Aug 01, 2004
Posts: 62
Why doesn't this work?

Gary Down
Ranch Hand

Joined: Apr 09, 2004
Posts: 46
Assuming you have correctly created a BadException class :-

e.g.
class BadException extends Exception {}

try this:
throw new BadException();

Gary.
Jack Kay
Ranch Hand

Joined: Aug 01, 2004
Posts: 62
Works beautifully! Thank you Gary down!
BUT, what's the difference between:

class ForestFireException extends Exception{}
AND
class ForestFireException extends Throwable{}
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html


Tony Morris
Java Q&A (FAQ, Trivia)
Jack Kay
Ranch Hand

Joined: Aug 01, 2004
Posts: 62


I couldn't find the answer anywhere in there. I skimmed pretty fast because all of it I already knew. I like to skim, but the things I'm looking for are lost.

Does anyone know what the difference between these two?
class ForestFireException extends Exception{}
class ForestFireException extends Throwable{}

I know it goes like this:
-Throwable
--Exception
---OtherExceptions

But I don't see why we should extend Exception, when extending Throwable works just as well.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Jack Kay:
But I don't see why we should extend Exception, when extending Throwable works just as well.


Basically its a matter of design. Check out the Javadocs for Exception and Error (the two subclasses of Throwable)

Exception: The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.
Error: An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

Also, from what I remember from my programmer's cert: All subclasses of Exception are "Checked exceptions" meaning that they must be explicitly handled either by a try/catch block or a throws clause. All direct subclasses of Throwable, Error or RuntimeException are "unchecked exceptions" and do not need to be explicitly handled.

So, the question is -- how do you want your exception to be dealt with? What kind of exception is it? Is it something expected taht can happen pretty often and people should need to handle it? or is it very rare and unexpected and when it happens, it means something is really wrong...


- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Everything that Jess said is correct except that direct subclasses of Throwable are actually checked, not unchecked.

At some level, you want people to be able to say "catch (Exception ex)" and catch only recoverable things; that's the main reason why user exceptions should generally extends Exception. If you extend Throwable, then your type can't be caught generically without also catching junk like OutOfMemoryError at the same time.


[Jess in Action][AskingGoodQuestions]
Weij Yin
Greenhorn

Joined: Aug 02, 2004
Posts: 9
If you extend Throwable, then your type can't be caught generically without also catching junk like OutOfMemoryError at the same time.


Hi Ernest,
Thanks for a good explanation from Jess and you. But I do not have a clear understand on your words obove. Could you explain a little more? Thanks in advance.

Weij


AspectJ, an asepct-oriented extension to Java programming language.<br /><a href="http://www.aspectj.org" target="_blank" rel="nofollow">http://www.aspectj.org</a>
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

How's this explanation:

You want to group your exceptions into logical subsets, so that by catching one type, you're catching all the exceptions that make sense.

So, say you're writing a poker game, and there are a few bad things that can go wrong during the game: you could shuffle poorly, you could Deal too many cards to a player, or someone could cheat. It makes sense that if any of these things happen -- you would deal with it appropriately. BUT -- if something like an OutOfMemoryError happened, you have no way of "fixing" that problem -- so you don't want to catch that Error along with those exceptions that you actually have a way of dealing with.



So, notice if one of the Exceptions happens that I know I can deal with (Bad deal, someone cheated etc.) I just restart the hand. But if some unknown Error occurred (i.e. something in the Throwable class) -- I exit the game.

Also don't forget, you can catch each exception individually:
Jack Kay
Ranch Hand

Joined: Aug 01, 2004
Posts: 62
Cool

So extending Throwable are mainly for those really hard errors that occur within the system, and then extending Exception is a bug in the program that can most likely be restored.

The OutOfMemoryError would be something that Throwable would deal with since that's one of the hard errors, and you'd most likely exit the program.

So you catch the ones you can restore (extends Exception), and then with the ones you can't fix, they would probably go to (catch (Throwable e)) with most likely a System.exit(1) ending.

I'm going Ok right?

----------------------------------

But earlier you quoted from Java docs about how Errors should not be caught. But if a serious error occurs, shouldn't you catch the Error, and then exit the program?
[ August 02, 2004: Message edited by: Jack Kay ]
Gary Down
Ranch Hand

Joined: Apr 09, 2004
Posts: 46
Maybe this link will assist in clarifying the relationships between throwable and exception
The Throwable class anthrowable.htmd it's subclasses

Gary.
Gary Down
Ranch Hand

Joined: Apr 09, 2004
Posts: 46
Well, talk about clarity,
that shuld read The Throwable class and it's sub classes

Gary.
Jack Kay
Ranch Hand

Joined: Aug 01, 2004
Posts: 62
I see now, thank you Gary, and everyone that went on this long journey lol
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I can't create my custom exceptions!?