1

Why is it so? This code compiles fine but throw an exception at run time

public class Except { public static void main(String args[]) { System.out.println("Hey, I am under main"); method(); } static void method() { throw new NullPointerException(); } } 

But this code isn't compiling

public class Expect { public static void main(String args[]) { System.out.println("Hey, I am under main"); method(); } static void method() { throw new IllegalAccessException(); } } 

Why this is so?That changing the type of exception,the second code is not compiling.Can somebody please explain,and I have used the throw statement in first code,and since its compiling fine,then what is the use of writing throws statement with method declaration. I am using jdk 1.8.0_45

3 Answers 3

6

NullPointerException is a sub-class of RuntimeException, which makes it an unchecked exception, which doesn't have to be handled or declared in a throws clause.

IllegalAccessException is a checked exception, so it must either be caught or be declared in the throws clause of any method that may throw it.

The second snippet will compile if you add a throws clause :

static void method() throws IllegalAccessException { throw new IllegalAccessException(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

,does the same happen with IOException??
@RajMalhotra check this link. You will find all classes, which (directly) inherit from RuntimeException.
@RajMalhotra yes, IOException is also a checked exception, like any sub-class of Exception that is not a sub-class of RuntimeException.
1
  1. Checked exceptions must be explicitly caught or propagated as described in try-catch Exception Handling. Unchecked exceptions do not have to be caught or declared thrown.

  2. Checked exceptions in Java extend the java.lang.Exception class. Unchecked exceptions extend the java.lang.RuntimeException.

Unchecked Exceptions List:

ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException IllegalStateException NullPointerException NumberFormatException AssertionError ExceptionInInitializerError StackOverflowError NoClassDefFoundError 

Checked Exceptions List:

Exception IOException FileNotFoundException ParseException ClassNotFoundException CloneNotSupportedException InstantiationException InterruptedException NoSuchMethodException NoSuchFieldException 

Comments

1

I want to add my two cents on the difference between checked and unchecked exceptions, from user's point of view.

Imagine a simple application, where user registers to a system providing name, lastname and a phone number.

Let's take a look at checked exceptions first. If user enters 12312312 for the name field, app should throw a checked exception.Similarly if user enters angelo as phone number, app again should throw a checked exception. Because neither 12312312 is a valid name for a person, nor is angelo as a phone number. We use checked exceptions in such cases. Because it is basically a user error and with proper feedback, user can correct this kind of errors.

On the other hand, unchecked exceptions were not user exceptions. For this kind of situations, there is simply NOTHING that user could do. For instance, connection to the database may get lost. Or When developer created the database field for above example, he might defined the name field in the database as a boolean field. (I am exaggerating here a bit).

I hope this helps too.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.