Is there a way to throw an exception without adding a throws declaration?

I have the following situation.

I have a Java class that inherits from another base class and overrides the method. The base method does not throw exceptions and therefore does not have a throws ... declaration.

Now my own method should be able to throw an exception, but I have a choice for

  • Swallow an exception
  • Add throw ad

Both do not satisfy, because the first one silently ignores the exception (normal, I could do some logging), and the second generated compiler errors due to different method headers.

 public class ChildClass extends BaseClass { @Override public void SomeMethod() { throw new Exception("Something went wrong"); } } 
+57
java exception-handling
Dec 23 2018-10-12
source share
9 answers

You can throw unchecked exceptions without declaring them if you want. Excluded exceptions are extended by RuntimeException . Throwables that extend Error are also unchecked, but should only be used for really serious problems (such as invalid bytecode).

As a specific case, Java 8 added an UncheckedIOException to wrap and rebuild an IOException .

+73
Dec 23 '10 at 14:26
source share

Here is the trick:

 class Utils { @SuppressWarnings("unchecked") private static <T extends Throwable> void throwException(Throwable exception, Object dummy) throws T { throw (T) exception; } public static void throwException(Throwable exception) { Utils.<RuntimeException>throwException(exception, null); } } public class Test { public static void main(String[] args) { Utils.throwException(new Exception("This is an exception!")); } } 
+34
Aug 23 '13 at 17:46
source share

The third option is to discard exception checking (as is usually the case with the standard API) and wrap the checked exception in a RuntimeException :

 throw new RuntimeException(originalException); 

You might want to use a more specific subclass of RuntimeException .

+17
Dec 23 '10 at 14:29
source share

I just want to add an alternative answer, just like FYI :

Yes, there is a way to throw a checked exception without adding a throws declaration using the sun.misc.Unsafe class. This is described in the following blog post:

Throw checked exception to method without declaration

Code example:

 public void someMethod() { //throw a checked exception without adding a "throws" getUnsafe().throwException(new IOException()); } private Unsafe getUnsafe() { try { Field field = Unsafe.class.getDeclaredField("theUnsafe"); field.setAccessible(true); return (Unsafe) field.get(null); } catch (Exception e) { throw new RuntimeException(e); } } 

However, this is not recommended. It is better to wrap an unchecked exception as described in some other answers.

+7
Dec 23 '10 at 14:45
source share

Why don't you throw an exception? This should not be announced.

Two alternatives:

  • wrap with checked exception with unchecked.
  • don't let the compiler know that you are throwing a checked exception, for example. Thread.currentThread () stop (e).
  • In Java 6, you can rebuild the exception if it is final , and the compiler knows which checked exceptions you could catch.
  • In Java 7, you can rebuild the exception if it is really final, i.e. you do not change it in the code.

Later it is more useful when you throw a verification exception in your code and catch it in your code code, but the layers between them do not know anything about the exception.

+3
Dec 23 '10 at 14:28
source share

Here is an example of catching checked exceptions and wrapping them in an unchecked exception:

 public void someMethod() { try { doEvil(); } catch (IOException e) { throw new RuntimeException(e); } } 
+1
Dec 23 '10 at 2:30 p.m.
source share

Yes, there is a reason, but it is not recommended to use it at all:

Unsafe Java Package

 getUnsafe().throwException(new IOException()); 

This method throws a checked exception, but your code did not force it to intercept or reverse engineer. As well as runtime exception.

+1
Oct. 16 '17 at 8:50
source share

you can catch an exception with a try-catch block in an overridden method. then you do not need to declare a throws-statement.

0
Dec 23 '10 at 14:29
source share

You can use any exception thrown from a RuntimeException or RuntimeException itself

or

use try block for exception throw code and handle it there

0
Dec 23 '10 at 14:40
source share