Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most common checked and unchecked Java Exceptions? [closed]

Tags:

java

exception

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.

like image 824
Brian Johnson Avatar asked Aug 11 '09 21:08

Brian Johnson


People also ask

Which are the common unchecked exceptions in Java?

Some common unchecked exceptions in Java are NullPointerException, ArrayIndexOutOfBoundsException and IllegalArgumentException.

Which are checked and unchecked exceptions in Java?

In Java, exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked. Consider the following Java program. It compiles fine, but it throws ArithmeticException when run. The compiler allows it to compile because ArithmeticException is an unchecked exception.

Which exception is better checked or unchecked?

Difference Between Checked and Unchecked Exceptions in JavaA checked exception is caught at compile time whereas a runtime or unchecked exception is, as it states, at runtime. A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled.


2 Answers

Unchecked Exception List
ArrayIndexOutOfBoundsException
ClassCastException
IllegalArgumentException
IllegalStateException
NullPointerException
NumberFormatException
AssertionError
ExceptionInInitializerError
StackOverflowError
NoClassDefFoundError

Checked Exception List
Exception
IOException
FileNotFoundException
ParseException
ClassNotFoundException
CloneNotSupportedException
InstantiationException
InterruptedException
NoSuchMethodException
NoSuchFieldException

like image 185
Praveen Kishor Avatar answered Sep 28 '22 16:09

Praveen Kishor


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

like image 26
Platinum Azure Avatar answered Sep 28 '22 16:09

Platinum Azure