java是一个异常方法,它记录并抛出异常,异常作为参数出现
我有一个Util类,在这个类中,我试图实现一个简单的方法,它记录即将到来的消息并抛出发送的异常。原因很简单,我没能做到
我目前的方法是这样的
public static void handleException( Exception exception, String errorMessage )
{
LOGGER.error( errorMessage + "\n " + exception.getMessage() );
throw new IllegalArgumentException( errorMessage + "\n " + aException.getMessage() );
}
然而,我想要的不仅是IllegalArgumentException,还有作为参数发送的异常类型(也称为异常)
public static void handleException( FooException exception, String errorMessage )
{
LOGGER.error( errorMessage + "\n " + exception.getMessage() );
throw new FooException( errorMessage + "\n " + aException.getMessage() );
}
哪种方式是实现这一目标的最佳方式
# 1 楼答案
你为什么不重述一下你一开始遇到的异常呢
抛出异常
如果这不符合您的需求,您可以尝试通过从异常中获取类并使用反射来创建新实例(可能会出错,因为可能存在不同的构造函数)
希望有帮助