[ 
https://issues.apache.org/jira/browse/LANG-1726?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17803335#comment-17803335
 ] 

Václav Haisman commented on LANG-1726:
--------------------------------------

After looking at this more recruflly, there are more problems than just the 
comment.

The bodies of the {{rethrow}} method and the {{asRuntimeException}} method are 
the same, yet for some reason they have different return types:
{code:java}
public static <T> T rethrow(final Throwable throwable) {
    // claim that the typeErasure invocation throws a RuntimeException
    return ExceptionUtils.<T, RuntimeException>eraseType(throwable);
}
public static <T extends RuntimeException> T asRuntimeException(final Throwable 
throwable) {
    // claim that the typeErasure invocation throws a RuntimeException
    return ExceptionUtils.<T, RuntimeException>eraseType(throwable);
} {code}
The motivating example above the {{asRuntimeException}} does not make sense 
with such return type, unless {{invocation()}} also returns 
{{{}RuntimeException{}}}:
{code:java}
*  public int propagateExample { // note that there is no throws clause
*      try {
*          return invocation(); // throws IOException
*      } catch (Exception e) {
*          return ExceptionUtils.rethrowRuntimeException(e);  // propagates a 
checked exception
*      }
*  } {code}
The motivating example above also uses {{{}rethrowRuntimeException{}}}, which 
does not exist at all.

There are other problems with the method. The method does not actually throw 
{{RuntimeException}} as its name suggests. But that is correct because the 2nd 
sentence of the method comment ("This method prevents throws clause pollution 
and reduces the clutter of "Caused by" exceptions in the stack trace.") 
suggests it should not be wrapping with anything.

I am not sure what exactly should be changed because I do not know the 
intentions you had with it. 

If it was to wrap and throw the passed {{Throwable}} with 
{{{}RuntimeException{}}}, it should actually do that, instead of rethrowing the 
original throwable, and it should have the same signature as the {{rethrow}} 
method to actually be useful in all the same contexts as the {{rethrow}} method 
is now. And the 2nd sentence «This method prevents throws clause pollution and 
reduces the clutter of "Caused by" exceptions in the stack trace.» should be 
removed because it becomes false.

 

> ExceptionUtils.asRuntimeException is not a good replacement for deprecated 
> ExceptionUtils.rethrow
> -------------------------------------------------------------------------------------------------
>
>                 Key: LANG-1726
>                 URL: https://issues.apache.org/jira/browse/LANG-1726
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.exception.*
>    Affects Versions: 3.14.0
>            Reporter: Václav Haisman
>            Priority: Major
>             Fix For: 3.15.0
>
>
> I have been doing pass over deprecated warning in our code and one of them is 
> that {{org.apache.commons.lang3.exception.ExceptionUtils#rethrow}} is 
> deprecated. Its comment says "Use asRuntimeException(Throwable)." However, 
> {{asRuntimeException()}} is not a good replacement. It lacks the generic type 
> parameter R that the rethrow method has. In fact, the comment that says...
> {noformat}
> Returns:
> Never actually returned, this generic type matches any type which the calling 
> site requires. [..]
> {noformat}
> ...is lying. 



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to