Rethrowing exceptions in Java without losing the stack trace

后端 未结 9 2112
挽巷
挽巷 2020-11-28 19:18

In C#, I can use the throw; statement to rethrow an exception while preserving the stack trace:

try
{
   ...
}
catch (Exception e)
{
   if (e is         


        
9条回答
  •  情深已故
    2020-11-28 20:04

    catch (WhateverException e) {
        throw e;
    }
    

    will simply rethrow the exception you've caught (obviously the surrounding method has to permit this via its signature etc.). The exception will maintain the original stack trace.

提交回复
热议问题