Tra*_*svi 5 java reflection exception-handling
我有一个方法,我想通过反思来调用.该方法对其参数进行了一些不同的检查,并且可以抛出NullPointer和IllegalArgument异常.
通过Reflection调用方法也会抛出需要捕获的IllegalArgument和NullPointer异常.有没有办法确定异常是由反射Invoke方法还是由方法本身引起的?
emo*_*ory 17
如果方法本身引发了异常,那么它将被包装在InvocationTargetException中.
您的代码可能如下所示
try
{
method . invoke ( args ) ;
}
catch ( IllegalArgumentException cause )
{
// reflection exception
}
catch ( NullPointerException cause )
{
// reflection exception
}
catch ( InvocationTargetException cause )
{
try
{
throw cause . getCause ( ) ;
}
catch ( IllegalArgumentException c )
{
// method exception
}
catch ( NullPointerException c )
{
//method exception
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6928 次 |
| 最近记录: |