2

I have overridden a method from parent and added a throws declaration on that method. It gave me error when I added throws Exception and throws FileNotFoundExceprion but worked with throws NullPointerException. What is the reason?

class Vehicle {
  public void disp() {
    System.out.println("in Parent");
  }
}

public class Bike extends Vehicle {
  public void disp()throws NullPointerException {
    System.out.println("in Child");
  }

  public static void main(String[] args) {
    Vehicle v = new Bike();
    v.disp();
  }
}
Manoj
  • 644
  • 12
  • 21
  • 7
    Because NullPointerException extends RuntimeException and this don't break the override – Silvinus May 31 '16 at 14:15
  • You can't throw a checked exception when you're overriding a method that doesn't declare that it throws it. – khelwood May 31 '16 at 14:17
  • Not sure why you're being downvoted. For someone who doesn't know the subtleties about checked vs. unchecked exceptions in Java, this can be very confusing. And I don't know that I would figure out what to google for in this case. – sstan May 31 '16 at 14:17
  • That also means you don't need to put `throws NullPointerException` even if you are throwing one in the method. RuntimeExceptions are for unrecoverable errors, generally and are unchecked. – Chill May 31 '16 at 14:18
  • 3
    @sstan possibly for "it gave me an error" – khelwood May 31 '16 at 14:19
  • @khelwood: Can't argue there :) – sstan May 31 '16 at 14:19

2 Answers2

1

NullPointerException is a so-called unchecked exception (because it extends RuntimeException), which means you can throw it anywhere without explicitly marking that the method "throws" it. On contrary, the other exceptions you posted are checked exceptions, which means that the method must be declared as "throwing" the exception or the problematic code must be invoked in a try-catch block. For example:

class Vehicle{
 public void disp() throws Exception {
    System.out.println("in Parent");
 }
}
public class Bike extends Vehicle {
 public void disp() throws Exception {
    System.out.println("in Child");
 }
 public static void main(String[] args) throws Exception {
    Vehicle v = new Bike();
    v.disp();
 }
}

...or:

class Vehicle{
 public void disp() throws Exception {
    System.out.println("in Parent");
 }
}
public class Bike extends Vehicle{
 public void disp() throws Exception {
    System.out.println("in Child");
 }
 public static void main(String[] args) {
    Vehicle v = new Bike();
    try {
      v.disp();
    } catch(Exception exception) {
      // Do something with exception.
    }
 }
}

You can find out more about Java exceptions here.

Community
  • 1
  • 1
Czyzby
  • 2,999
  • 1
  • 22
  • 40
0

Conceptually in Java there are two types of exceptions:

  • checked exceptions
  • unchecked exceptions

These are used to indicate different things. A checked exception is a special condition which may happen and you are forced to handle this. A FileNotFoundException for example is a situation which may arise (for example you are trying to load a file that doesn't exist yet).

In this situation these are checked because your program is supposed to handle them.

On the opposite side an unchecked exception is a situation which shouldn't normally occur during the execution of a program, a NullPointerException means that you are trying to access a null object, which shouldn't never happen. So these exceptions are more likely bugs in software that may arise everywhere, you are not forced to declare what's throwing them and handling them is optional according to the requirements.

By following your bike analogy it's like having a FlatTireException on your Bike class. It could be a checked exception because it's a situation which may arise and which should be handled, while a WheelMissingException is something that shouldn't happen.

Jack
  • 131,802
  • 30
  • 241
  • 343