1

why overriding methods can throw unchecked exception in java ?

Why can't overriding methods throw exceptions broader than the overridden method? Is not my question . I just want to know why Overriding methods CAN throw unchecked exception while cannot throw checked exception.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
supraja
  • 93
  • 2
  • 11
  • Why can't overriding methods throw exceptions broader than the overridden method? Is not my question . I just want to know why Overriding methods CAN throw unchecked exception while cannot throw checked exception. – supraja Jul 05 '15 at 07:54
  • @suraja Are you asking why an overriding method can throw an unchecked exception or are you asking why an overriding method can throw an unchecked exception that is broader than the exception thrown by the overriden method but that's not the case when it comes to checked exceptions? Perhaps some code would help to understand what you are trying to ask? – Chetan Kinger Jul 05 '15 at 09:08
  • You override a method `int foo()`. You introduce a bug by calling a method on a null reference. What should the JVM do? Return 0? -1? Or throw a NullPointerException to signal the problem? – JB Nizet Jul 05 '15 at 09:13
  • 1. Because unchecked exceptions aren't checked. 2. Because it would violate the contract of the overridden method. – user207421 Jul 05 '15 at 09:33

2 Answers2

3

Let's say a base class has the following method:

int foo() throws IOException;

That means that the method can

  • return an int
  • throw a checked IOException
  • throw any runtime exception it wants, because runtime exception don't need to be declared in the throws clause

Now let's override this method in a subclass. The subclass must obey the contract of the base method, so it can

  • return an int
  • throw a checked IOException
  • throw any runtime exception it wants: that wouldn't violate the contract of the base method.

What it can't do, though, is

  • returning a double, an object, or anything other than int, because that would violate the contract of the base method
  • throw another checked exception, because that would violate the contract of the base method
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • This does not answer the question. I am afraid the OP did not ask *What are the rules with respect to checked and unchecked in case of overriden methods?* but rather *Why are these rules imposed?* This is apparent from the statement in the question that reads *why overriding methods can throw unchecked exception in java?*. The answer in my opinion is not *because that would violate the contract of the base method*. The actual question (the *why* part) has not been answered. – Chetan Kinger Jul 05 '15 at 09:51
  • It has been answered, in the very beginning of the question: a method can throw any runtime exception it wants, and overriding methods also can, since that wouldn't violate the base method contract. – JB Nizet Jul 05 '15 at 09:54
  • *because runtime exception don't need to be declared in the throws clause* . This only explains the syntax. The actual question to be answered is *Why is there no requirement for runtime exceptions to be declared in the throws clause?* Why are they not a part of the contract? These are the actual questions to be answered. – Chetan Kinger Jul 05 '15 at 09:58
  • Well, the OP and the other readers will judge about that. If he/she/they want, they can upvote your answer rather than mine. – JB Nizet Jul 05 '15 at 10:02
1

why overriding methods can throw unchecked exception in java ?

Any method in Java can throw an unchecked exception. An overriding method is also a method so it gets the same privileges as other methods when it comes to unchecked exceptions.

I just want to know why Overriding methods CAN throw unchecked exception while cannot throw checked exception

The last part of your sentence is incorrect. An overriding method can throw both checked an unchecked exceptions. I believe the questions that you are trying to ask are

  1. Why CAN'T an overriding method throw a checked exception when the overriden method doesn't.
  2. Why CAN an overriding method throw an unchecked exception when the overriden method doesn't.

Let's answer 1)

   class Parent {
      public void doSomething() { }
   }

   class Child extends Parent {
      public void doSomething()throws Exception { }
   }

Let's assume that the above code was allowed. The client code would then look like this :

   Parent p = new Child();
   p.doSomething(); 

If there was no restriction on what checked exceptions could be thrown by an overriden method, there would be no compilation error in the above code. The compiler has no idea that doSomething in Child can actually throw a checked Exception. The throw Exception clause for the doSomething method in Child would kind of be pointless since the programmer is not required to handle the checked exception.

Let's answer 2)

The Oracle tutorial has a nice write-up that is useful in the context of your question :

Runtime exceptions represent problems that are the result of a programming problem, and as such, the API client code cannot reasonably be expected to recover from them or to handle them in any way. Such problems include arithmetic exceptions, such as dividing by zero; pointer exceptions, such as trying to access an object through a null reference; and indexing exceptions, such as attempting to access an array element through an index that is too large or too small.

Runtime exceptions can occur anywhere in a program, and in a typical one they can be very numerous. Having to add runtime exceptions in every method declaration would reduce a program's clarity. Thus, the compiler does not require that you catch or specify runtime exceptions (although you can).

Since the programmer is not obliged to catch a RuntimeException that is thrown by a method, putting the same restriction as checked exceptions for unchecked exceptions in case of overriden methods would be pointless.

user207421
  • 305,947
  • 44
  • 307
  • 483
Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82