Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "exception handling" Watch "exception handling" New topic

exception handling

aditya kumar

Joined: Apr 02, 2008
Posts: 12
How can I throw an exception without declaring it in a 'throws' clause?
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Throw an exception that extends RuntimeException

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Are you really sure you wnat to throw an unchecked exception?

Would an Error be better?

I have to admit to feeling exceptions are used rather to much in Java, that sometimes what is an Exception actual should be an Error cos there is no way to recover from it. But I do not agree with C# method of no checked exceptions.

Check out the Java Tutorial on Exceptions, heres a discussion of Unchecked Exceptions, from the Exception tutorial.

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Gavin Tranter:
Are you really sure you wnat to throw an unchecked exception?

Would an Error be better?

Throwing Errors is very rarely a good idea, and certainly not something for beginners to do.

The idea of the distinction between RuntimeException and Error is that the former is evidence of a bug in a specific piece of code, whereas an Error is evidence of a JVM-wide problem. Examples: -

NullPointerException typically means that a particular thread has encountered a bug in a particular piece of code. But it might well be possible, and sometimes appropriate, for other threads to carry on running other pieces of code.

But OutOfMemoryError means that the whole JVM has run out of memory, and is in a state where data may be inconsistent. There is no way that any useful work can be done, once this has happened, and the only valid action is to try to close down the application as gracefully as possible.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
I agree. Here's the link:
subject: exception handling
It's not a secret anymore!