Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

exception

I have the following situation.

I have a Java Class that inherits from another base class and overrides a method. The base method does not throw exceptions and thus has no throws ... declaration.

Now my own method should be able to throw exception but I have the choices to either

  • Swallow the exception or
  • Add a throws declaration

Both a not satisfying because the first one would silently ignore the exception (ok I could perform some logging) and the second would generate compiler errors because of the different method headers.

public class ChildClass extends BaseClass {          @Override          public void SomeMethod() {             throw new Exception("Something went wrong");         } } 
like image 502
Jürgen Steinblock Avatar asked Dec 23 '10 14:12

Jürgen Steinblock


People also ask

How do you throw an exception without throws?

Without using throws When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects). If you re-throw the exception, just like in the case of throws clause this exception now, will be generated at in the method that calls the current one.

Can we catch exception without throw?

You can only catch C++ exceptions which were throw n. You have an extra } bracket after try block.. On Windows, if you pass the /EHa option to the compiler, you can catch access violations (and other Windows SEH exceptions) as C++ exceptions. But this is rarely a good idea.

What happens if you throw an exception without catching it?

You can avoid catching an exception, but if there is an exception thrown and you don't catch it your program will cease execution (crash). There is no way to ignore an exception.

Is it possible to throw an exception?

Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, it's always thrown with the throw statement.


2 Answers

You can throw unchecked exceptions without having to declare them if you really want to. Unchecked exceptions extend RuntimeException. Throwables that extend Error are also unchecked, but should only be used for completely un-handleable issues (such as invalid bytecode or out of memory).

As a specific case, Java 8 added UncheckedIOException for wrapping and rethrowing IOException.

like image 116
OrangeDog Avatar answered Sep 18 '22 17:09

OrangeDog


Here is a 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!"));     } } 
like image 24
Eng.Fouad Avatar answered Sep 21 '22 17:09

Eng.Fouad