Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it fine to throw an Exception as it is or wrap it  RSS feed

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I ran the PMD tool against my application and it says that
we should not..



but it should be thrown as user defined exception that are subclasses of Exception..

This has been categorized as severity 1 Error

Is it a problem if we throw a new Exception...or is it that we need to wrap it with a user defined ..??

Thanks in advance
Regards
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is generally bad to throw a plain Exception, because the calling code cannot tell what kind of thing went wrong. If your application is anything other than a small, throw-away utility, you should not throw plain Exception.

If there is an existing subclass of Exception in the Java API that well describes the problem, you can throw that. Some people don't like to do so, preferring to keep Java's own exceptions thrown only by Java. I'm not sure, myself.

If you can't or won't use an existing subclass of Exception from the Java API, then you should create your own. The name of the class should reflect what has gone wrong e.g. InvalidCustomerIDException. That is much better than, say, MyCompanyException.

You should also consider whether you want a checked or unchecked exception. If you want unchecked, subclass RuntimeException, not Exception. An unchecked exception should normally only be used for exceptional conditions arising from programming bugs, rather than from bad data from the program's user (e.g. mis-typed customer ID), or system problems (e.g. out of disk space).
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not illegal to throw an instance of Exception (although the code you've shown is not legal - it is missing brackets and a semi colon) but it's not good practice.

The whole idea of the exception mechanism is that the problem that has occured (ie the one that caused an exception to be thrown) can be identfied by the type of exception thrown and specific types of problem (exception) can be caught and handled appropriately. If all of the code throws instances of Exception when something goes wrong then it becomes incredibly difficult to know what has caused the problem and whether it can be handled or not.

EDIT: Sorry Peter I hadn't spotted that you'd already replied to this
[ August 18, 2007: Message edited by: Tony Docherty ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm usually pretty much an OO and good code fanatic, but this is one area I have been more relaxed, so far without any pain.

The exact type of the exception thrown is generally of interest to me only if I'm going to take some action based on that type. For example, if the database throws a particular exception maybe I'll store data in a flat file instead. But if there's no logic like that, I'm happy to declare throws Exception and even catch Exception.

Others will doubtless remind me that this is a "bad practice" but I define bad as something that I regret later, and as I said, this one hasn't caused me enough pain to worry about. Yet.

Back to the question about wrapping or converting the exception ... there are times you want to do this. For example, if I have a generic DataAccessor interface, I'll declare it to throw a DataAccessException. I don't want the database implementation to throw SQLException and the file implementation to throw IOException and the messaging implementation to throw something else. I'd have to change the signature every time I added a new implementation, and it would temp the caller into doing something based on exception type, blowing away the goodness of encapsulation and information hiding.
[ August 17, 2007: Message edited by: Stan James ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!