0

I have a question regarding java.lang.Exception and the way it is considered within the throw/throws clauses.

I have the following 2 examples and I don't understand exactly how the java.lang.Exception is handled: as checked or unchecked exception.

The following method compiles successfully:

public double method1() throws Exception {
   return 0.7;
}
  • here it seems to me that the java.lang.Exception is threaten java.lang.RuntimeException or java.lang.Error. You can declare to throw it even if you don't handle it.
  • if instead of an Exception we used a checked exception (that are sub-classes of java.lang.Exception) then you should had to have a throw declaration within the body of the method (or call a method that throws that checked exception). Right?

The following doesn't compile:

private void method2(int age) {
   if (age > 30)
       throw new Exception();
}
  • here is my confusion...if it was throwing a subclass of type java.lang.RuntimeException or java.lang.Error it would had compiled.
  • why in this case it doesn't compile ? I guess it is because it threats java.lang.Exception as a checked exception. But if it is so, then why method1 does compile?

Thank you, Luisa

  • Possible duplicate of [Java: checked vs unchecked exception explanation](http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation) – Burkhard Apr 05 '17 at 05:57

7 Answers7

2

If it was throwing a subclass of type java.lang.RuntimeException or java.lang.Error it would had compiled?

Yes, RuntimeException is unchecked Exception (or Error), so you don't need to declare it on the method signature or catch it explicitly. Like, for example, NullPointerException, you can't tell when that might happen in the code at Runtime (ie., because of which issue, so, you don't need to explicitly deal with it everywhere inside the code), if it happens at runtime because of any reason, you will see the exception stacktrace.

But, one point is that, you can catch (if you want) RuntimeException (using catch) and handle it, if you wanted to deal with the things differently upon raising that exception, you can look below on this.

try {
  //some code
} catch(ArithmeticException exe) {
   //handle it and you can do things differently how you like
}

Also, the same rule applies even for throws as well, i.e., if you wanted to, you can specify the throws ArithmeticException (or any RuntimeException) on the method signature, but it does not make any difference to the caller, rather it will clutter the code and it is NOT the best practice, you can look here on this. Whereas when you specify the throws caluse on the method for checked Exception, then the caller is forced to deal with it i.e., the caller method has to explicitly handle it using catch block or throw it again.

why in this case it doesn't compile? I guess it is because it threats java.lang.Exception as a checked exception. But if it is so, then why method1 does compile?

Yes, there are two rules for dealing with checked Exceptions:

(1) catch it inside the method using a catch block

(2) or throw it from the method (using throws in the method signature)

i.e., If your code block throws a checked exeption then either you need to catch it inside the method or else the method needs to explicitly declare that it throws that Exception.

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67
2

In first method 1),

public double method1() throws Exception 
{
   return 0.7;
}

you are propagating Exception [i.e. Checked Exception] to the calling method that is why you are not seeing compile time exception. let me show you something which can make you understand better,

  public void call() {
    method1(); // Because method1() is propagating compile time exception you must need to handle it here. 
  }

  public double method1() throws Exception  {
     return 0.7;
  }

In above code, call() you will get compile time exception.

In the second method 2)

private void method2(int age) {
   if (age > 30)
       throw new Exception(); //I am assuming you forgot to mention new in your code.
}

you are throwing checked exception so you must need to handle it. If instead you will change Exception() to RuntimeException so you will not see error at compile time. like below,

private void method2(int age) {
       if (age > 30)
           throw new RuntimeException(); // you will not get compile time error.
    }

for more details, http://technostepup.com/CoreJava/ExceptionHandling/exception-types.php

1

here it seems to me that the java.lang.Exception is threaten java.lang.RuntimeException or java.lang.Error. You can declare to throw it even if you don't handle it.

if instead of an Exception we used a checked exception (that are sub-classes of java.lang.Exception) then you should had to have a throw declaration within the body of the method (or call a method that throws that checked exception). Right?

No. You can declare you throw any exception (checked or unchecked) and not throw any.

This makes sense for example when the method is overridden in a subclass and you want the subclass to be able to throw checked exceptions. Overriding methods can throw only the checked exceptions the parent class declares.

Community
  • 1
  • 1
Joni
  • 108,737
  • 14
  • 143
  • 193
0

The method needs to be like this for compilation :

private void method2(int age) throws Exception
{
    if (age > 30)
        throw new Exception();
}

However, checked exceptions are a way of being able to act on that exception and do something meaningful such as freeing up resources etc and you will know what to do with that exception only when you throw actual subclass of Exception.

PankajT
  • 145
  • 3
0

In method2 you miss new keyword at line

throw Exception();

In java all checked exceptions must be declared with throws declaration and unchecked may not be declared.

Checked exceptions are those inherited from java.lang.Excetion class. But unchecked exceptions inherit java.lang.RuntimeException class.

Jay Smith
  • 2,331
  • 3
  • 16
  • 27
0

Throws clause in used to declare an exception and thow keyword is used to throw an exception explicitly. So when you write

private void method2(int age) {
    if (age > 30)
      throw Exception();
 }

You are throwing an exception explicitly but not declaring that the method can throw this exception using throws clause.

So the correct code will be

private void method2(int age) throws Exception {
if (age > 30)
    throw new Exception();
   }
Pooja Arora
  • 574
  • 7
  • 19
0

In your first example, if you call that method you will have to handle the exception it throws, because it's a checked one. If the method threw an unchecked exception you would not need to handle it.

Your second example does not compile because you are trying to throw the Exception class. You must throw an instance of it instead. If in that code you just change Exception to RuntimeException or Error or a subclass of them it would still not compile, for the same reason.

SantiBailors
  • 1,596
  • 3
  • 21
  • 44