Method implementation for this case in Java

I just saw a piece of code:

private static class DefaultErrorHandler<RT> implements ErrorHandler<RT> {
  public RT handle(Object[] params, Throwable e) {
   return Exceptions.throwUncheckedException(e);
  }
}

Now I wonder what the static method throwUncheckedException (Throwable e)will return exactly and how it can be implemented with respect to generics.

Can someone give me an example?

+3
source share
3 answers

You define a method as follows:

public static <T> T throwUncheckedException (Throwable e) { ... }

which essentially means "for any type T, return it." Then you rely on the compiler's ability to guess what T = RT.

, throwUncheckedException : , , -void-. Java , . " ".

+4

, :

throw new RuntimeException(e);

, , . "throw".

- , - RT, .

+1

throwUncheckedException(), -, , .

:

public RT handle(Object[] params, Throwable e) {
    throw new RuntimeException(e);
}

return ( ), , , , .

, handle , statememt.

throwUncheckedException() -, return.

throwUncheckedException() - :

public static <T> T throwUncheckedException(Throwable e) {
    throw new RuntimeException(e);
    return null;
}

, , , , , . , :

public RT handle(Object[] params, Throwable e) {
    throw Exceptions.makeUncheckedException(e);
}
0

Source: https://habr.com/ru/post/1737716/


All Articles