This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes When we create custom Error and when to create Custom RuntimeException? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "When we create custom Error and when to create Custom RuntimeException?" Watch "When we create custom Error and when to create Custom RuntimeException?" New topic
Author

When we create custom Error and when to create Custom RuntimeException?

tarek helmy
Ranch Hand

Joined: Nov 14, 2008
Posts: 42
When we create custom Error and when to create Custom RuntimeException?

please explained
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
what do you mean by 'custom' ?? You mean just throwing a RuntimeException or error or creating your own exception classes ?

Generally, it's always better to work with exceptions that are already contained in the jdk, since this is what other developers are already used to and makes your code easier to handle.

As for errors, don't throw them. They're to be used by the JVM when something really really messed up happens, such as OutOfMemoryError, StackOverflowError or stuff like that.


JDBCSupport - An easy to use, light-weight JDBC framework -
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3016
    
  10
Sebastian Janisch wrote:Generally, it's always better to work with exceptions that are already contained in the jdk, since this is what other developers are already used to and makes your code easier to handle.

I agree with the "generally" and reject the "always" - since a sentence with "always" is almost always incorrect.

I would say, try to use an exception from the standard Java libraries, if you can find one that's applicable to your situation. Or, try to find an applicable exception from whatever other third-party library or libraries you're using in the same part of the code where you want to throw an exception. But if none of these seems to apply, do go ahead and create a custom exception class, sure. Especially if you're reporting a particular situation that the caller might want to handle differently than they handle a generic "something bad happened" exception.
 
 
subject: When we create custom Error and when to create Custom RuntimeException?