java SecurityManager checkPermission(Permission perm)的异常处理问题

时间:2013-08-15 12:26:00

标签: java security exception-handling

为什么这不会因为try catch或者抛出方法而大喊大叫?

import java.security.Permission;

public class NewSecurityManager extends SecurityManager{

    public void checkPermission(Permission perm) {
        throw new SecurityException("You are drunk. Please go home!");
    }

}

2 个答案:

答案 0 :(得分:3)

SecurityManager扩展RuntimeException,使其成为unchecked exception。 未经检查的异常不需要包含try/catch块或要从包含方法重新抛出异常。

答案 1 :(得分:3)

java.lang.Object
  java.lang.Throwable
      java.lang.Exception
          java.lang.RuntimeException
              java.lang.SecurityException

如上所示,SecurityException是RuntimeException的子代,因此您不需要任何throw子句。只有被检查的异常才会被处理,而不是未经检查的(运行时)