• Post Reply Bookmark Topic Watch Topic
  • New Topic

method throwing runtime exception explicitely...  RSS feed

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In what cases, we should write such method in real projects:



Thanks.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
In what cases, we should write such method in real projects:



Thanks.


To an extent, it would depend on the particular runtime exception. You can decide to use, for example, IllegalArgument exception to signal inappropriate parameters. Other useful RuntimeExceptions I've explicitly thrown are:

ClassCastException
IndexOutOfBoundsException
MissingResourceException
NoSuchElementException
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bruce Eckel offers some interesting perspective in this section of his TIJ exception chapter.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The comments in TIJ are remarkably balanced for this "hot button" issue. Eckel nicely explains how he has gradually changed his own views.

As an experiment in style I wrote one non-trivial system where public methods never throw checked exceptions. Instead they throw an application-specific exception that extends RuntimeException. It worked out well with the structure of the system for exceptions to bubble up to the top layer of the architecture so try-catch clauses are rare in the code. If I had to take different actions based on different exceptions in a lot of places I'd probably use the more conventional checked style. Maybe.
 
Hemant Agarwal
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really this is a nice discussion
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!