萌萌菌吧 关注:372贴子:3,822
  • 13回复贴,共1

@1003917294 我来装逼辣 #(滑稽)

只看楼主收藏回复

请提供一种方式,可以让Java抛出经过检查的异常而不需要try-catch或者声明在throws子句
注意:
①不能使用已过时方法(方法上标注@deprecated)
②使用Java编写而非JVM字节码
③禁止编译后修改class文件或者dex文件
④不需要适应所有的异常
⑤在未经过检查的异常中包含一个经过检查的异常算作未经过检查的异常
我来提供一种方法(使用Thrower.sneakyThrow(Throwable)抛出经过检查的异常):
class Thrower
{
private Throwable e;
private Throw() throws Throwable
{
throw e;
}
public static synchronized void sneakyThrow(Throwable e)
{
Throw.e=e;
try
{
Throw.class.newInstance();
}
catch(InstantiationException t)
{
throw new IllegalArgumentsException();
}
catch(IllegalAccessException t)
{
throw new IllegalArgumentsException();
}
finally
{
Throw.e=null;
}
}
}


IP属地:上海来自Android客户端1楼2015-07-13 11:35回复
    @1003917294


    IP属地:上海来自Android客户端2楼2015-07-13 11:35
    回复
      2025-06-01 07:23:11
      广告
      如果能到50楼,我就发使用@deprecated的方法,如果到100楼,我就发本题答案


      IP属地:上海来自Android客户端3楼2015-07-13 11:36
      回复

           --闭上双眼我又看见,当年那如梦的画面


        IP属地:浙江来自Android客户端4楼2015-07-13 11:37
        收起回复


          IP属地:浙江来自Android客户端5楼2015-07-13 12:11
          回复
            在main方法抛出异常


            IP属地:河北来自Android客户端6楼2015-07-13 17:47
            收起回复