Java best practices when throwing exceptions: throwing core Java exceptions

Jon Onstott :

Instead of throwing new Exception("Some message", maybeSomeCause), which means that all callers of my method will need to catch Exception (which can include RuntimeExceptions), I'd like to throw a more specific type of exception when a problem occurs.

I can create my own exception types which extend Exception or another exception type, but I am curious if it is a good idea to re-use some exceptions that come with core Java language, such as:

  • IllegalArgumentException
  • UnsupportedOperationException
  • IOException
  • Others?

Are there others that I am missing? I found a basic list of the 'core' exceptions here: http://rymden.nu/exceptions.html, with humous explanations.

Thanks!

Edit:

Is there a good list of 'core' exceptions?

List so far:

Jeffrey Blattman :

Yes, it's very good to do that. In fact, it's even written about in Effective Java, 2nd ed. See item 60 on page 248: "Favor the use of standard exceptions"

Reusing preexisting exceptions has several benefits. Chief among these, it makes your API easier to learn and use because it matches established conventions with which programmers are already familiar. A close second is that programs using your API are easier to read because they aren’t cluttered with unfamiliar exceptions. Last (and least), fewer exception classes mean a smaller memory footprint and less time spent loading classes.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive