0

I'm using the IDE Netbeans7.3 to develop in Java. There is something strange that I cannot explain to myself, so please, help me to understand.

I declared to classes. The first inherits from Exception:

public class MyParameterException extends Exception{        
    public MyParameterException(){
        super();
    }        
    public MyParameterException(String message){
        super(message);
    }
}

and the second inherits from NullPointerException:

public class NullMyParameterException extends NullPointerException{
    public NullMyParameterException(){
        super();
    }        
    public NullMyParameterException(String message){
        super(message);
    }
}

Now, when I create a method in a class and I write:

public void test(String s){
    if(s==null) throw new NullMyParameterException("The input string is null."); 
    if(s.trim().isEmpty()) throw new MyParameterException("The input string is empty.");
}

What seems strange to me is that I get the message unreported exception MyParameterException must be caught or declared to be thrown from the IDE, but nothing is said about the first exception that I could throw in the method.

To my knowledge, the method would expected to be declared as follows:

public void test(String str) throws MyNullParameterException, MyParameterException

but for Netbeans only sufficies:

public void test(String str) throws MyParameterException

Is this:

  • An IDE bug.
  • Normal because classes that inherits from NullPointerException are special.
  • ...

Please, let me understand.

mat_boy
  • 12,998
  • 22
  • 72
  • 116
  • please see this too http://stackoverflow.com/questions/2699580/difference-between-unchecked-exception-or-runtime-exception – tgkprog Apr 10 '13 at 08:41

3 Answers3

2

Read about runtime exceptions and regular exceptions. http://docs.oracle.com/javase/6/docs/api/java/lang/RuntimeException.html

Runtime exceptions are not checked by the compiler. The IDE uses the compiler to get this information. Will see same output if you compile from command prompt

You should see this too Difference between Unchecked exception or runtime exception

Community
  • 1
  • 1
tgkprog
  • 4,493
  • 4
  • 41
  • 70
1

you are not warned about NullPointerException - it is unchecked exception, buy your exceptions should be checked. You should change your program, NullMyParameterException should extends Exception aswell, and you should declare that these exceptions will be thrown in method by:

public void test(String s) throws MyParameterException, NullMyParameterException
Miloš Lukačka
  • 842
  • 1
  • 11
  • 25
  • Doesn't NullPointerException extends Exception too? Why should I do that? Can I extends two classes? :) – mat_boy Apr 10 '13 at 08:01
  • NullPointerException extends Exception, but it is unchecked exception - almost every method in java would have had throws NullPointerException in decleration. Your exceptions should be checked, that's why are you implementing them, to prevent throwing unchecked exception - because that would crash the program – Miloš Lukačka Apr 10 '13 at 08:04
  • you should extend only Exception with both your exceptions – Miloš Lukačka Apr 10 '13 at 08:05
1

It is normal. When you extends RuntimeException (NPE in your case) you don't need to declare the method as throwing it.

For checked exceptions (MyParameterException in your case), you must declare the method as throws MyParameterException in order to throw it.

You can read more about checked vs unchecked exceptions

Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61