views:

249

answers:

7

As far as I understand, there is no way to find out which Exceptions a method throws without looking up the API docs one-by-one. Since that is no option, I'd like to reverse the research and ask you which are the most common Exceptions and RuntimeExceptions you've come across when dealing with

  • Casting
  • Arrays
  • Vector, ArrayList, HashMap, etc.
  • IO (File class, streams, filters, ...)
  • Object Serialization
  • Threads (wait(), sleep(), etc.)
  • or anything else that is considered "basic Java"

I realize that this might be subjective and boring but it is for a class test and I really don't know better.

+4  A: 

NullPointerException

Gandalf
+1  A: 

Checked exceptions are easy, your editor should display the javadocs when you hover/complete the method name.

Unchecked are generally actual errors and aren't even in the javadocs more often than not. I guess the most common might be IllegalArgumentException, any method that has any possible combination of parameters that is invalid should throw it.

Bill K
If he's using a text editor, it won't display javadocs on method name completion necessarily. :-(
Platinum Azure
I think you mean IllegalArgumentException.
Mike Daniels
Don't use a text editor. (Trust me, I did it for decades). It's like letting the air out of your bicycle tires because you need more exercise.
Bill K
For Java, I can understand that, but I'm predominantly a C/Perl programmer on Unix and I practically live on vim. *shrug* I suppose if you thought I had meant something that's completely useless, like (say) Notepad, then I would agree with you. But vim does have a number of useful features that allow me to be ridiculously fast in it, whereas even something as useful as Visual Studio with its name completion is still slower for me since I have to use the mouse for a lot of operations. Maybe I'll discover decades later that IDEs are better in general (and not just for Java or C#), who knows.
Platinum Azure
And it's a mark of how new I am here that I think I can get away with using stars to denote an action. Oops. Wonder if semicolons will work instead? ::shrug:: Ah well, who cares. Sorry for the comment flood, won't happen again.
Platinum Azure
There is a vi mode for just eclipse, netbeans and probably any environment you can come up with, but vi does not have ctrl-space!
Bill K
@Platinum: By the way--look at all the code you typed last week. Print it out. Take the paper and start over typing it in. It'll probably take you 10 minutes, less than an hour for sure. In notepad it might take 10% longer, but more likely will be just as fast. so you're talking about maybe a few minutes of your week as Ridiculously Fast. Once I started to think in these terms, I had to reconsider a lot of "Efficiency practices" (for instance, you also realize that pair programming is not at all wasteful, typing is actually SO LITTLE a part of what you do; if it's not refactor more!)
Bill K
+1  A: 

How about looking for subclasses of java.lang.exception, for example here

Personally I use 2 checked exceptions of my own TransientException for cases when a retry might work. And InvalidRequestException for validation errors.

djna
+1  A: 

NumberFormatException

Firstthumb
A: 

IllegalArgumentException is one that crops up often.

Jon
+3  A: 

Assume the below are java.lang unless I specify otherwise:

Casting: ClassCastException

Arrays: ArrayIndexOutOfBoundsException, NullPointerException

Collections: NullPointerException, ClassCastException (if you're not using autoboxing and you screw it up)

IO: java.io.IOException, java.io.FileNotFoundException, java.io.EOFException

Serialization: java.io.ObjectStreamException (AND ITS SUBCLASSES, which I'm too lazy to enumerate)

Threads: InterruptedException, SecurityException, IllegalThreadStateException

Potentially common to all situations: NullPointerException, IllegalArgumentException

You would do well to look at Java site's Package Summary pages. Here's one: http://java.sun.com/j2se/1.4.2/docs/api/java/io/package-summary.html

Platinum Azure
I never realized that there is indeed an "Exception summary" down there. Thanks!
(Edited to make the exception list readable)No problem, I only just discovered it myself by accident. How great it is, that "when one teaches, two learn", eh?Sorry for being a little snippy earlier, I'll edit to fix that shortly.
Platinum Azure
+1  A: 

As Bill K says. Checked exceptions are easy. If your IDE/program editor doesn't give you an quick way to see method javadocs or signatures you need to throw it away. Seriously.

Unchecked exceptions are a different kettle of fish. But I think the best strategy with unchecked exceptions is to not try to catch them. Instead, you write you code so that it avoids throwing them in the first place. For example;

// ... not sure if 'obj' is null
if (obj != null) {
    obj.someMethod();
}
// ... not sure if 'obj' has the right type
if (obj instanceof Foo) {
    Foo foo = (Foo) obj;
}
// ... not sure if 'i' is in range
if (i >= 0 && i < array.length) {
    .... = array[i];
}

Here's why I recommend this:

  • A guard test is orders of magnitude more efficient than throwing and catching an exception.
  • A guard test is more readable ... less lines of code.
  • If you catch an unchecked exception, you can never be sure that it happened for the reasons you think it did; for example:
    // obj might be null ...
    try {
        obj.doSomething();
    } catch (NullPointerException ex) {
        System.err.println("obj was null");  // WRONG!!!
        // the NPE could have happen inside doSomething()
    }
  • If an unchecked exception is caused by a bug, you DO want a stacktrace and (depending on the application) you MAY NOT want to recover.

Obviously, you only include these "guard" checks where your understanding of the code tells you that they are necessary! So, for example if you know that 'obj' ought to be non-null and 'i' ought to be in range, it is a good idea to leave the checks out. If you leave out one test too many, you'll get an exception ... but that's good because you can then use the stacktrace to figure out why your understanding of the code was wrong, and maybe fix the underlying bug.

Stephen C
Very thorough answer.
Platinum Azure