namalfernandolk
namalfernandolk

Reputation: 9134

Exception handling issue with java SecurityManager checkPermission(Permission perm)

Why this doesn't yell to surround with try catch or throw from mathod?

import java.security.Permission;

public class NewSecurityManager extends SecurityManager{

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

}

Upvotes: 0

Views: 385

Answers (2)

Reimeus
Reimeus

Reputation: 159844

SecurityManager extends RuntimeException which causes it to be an unchecked exception. Unchecked exceptions dont require enclosing try/catch blocks or the exception to be rethrown from the containing method.

Upvotes: 3

M Sach
M Sach

Reputation: 34424

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

As you can see above SecurityException is the child of RuntimeException so you dont need any throw clause. Only checked exception are forced to deal with , not unchecked(Runtime) ones

Upvotes: 3

Related Questions