
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - SecurityException
The Java SecurityException is thrown when a security manager detects a security violation. The SecurityException is a runtime exception that is thrown when a security manager detects a security violation.
For example, if a program tries to access a file that it does not have permission to access, the security manager will throw a SecurityException.
There are some scenarios when JVM throws a SecurityException in Java, two of them are following:
- When the user tries to access a file that it does not have permission to access, JVM throws a SecurityException.
- When the user tries to access a system property that it does not have permission to access, JVM throws a SecurityException.
Constructors of SecurityException
There are two constructors of SecurityException class:
- SecurityException(): This constructor is used to create a SecurityException object without any message.
- SecurityException(String message): This constructor is used to create a SecurityException object with a message.
Methods of SecurityException
There are some methods of SecurityException class:
Method | Description |
---|---|
getMessage() | It is used to return the message of the exception. |
toString() | It is used to return the detail message string of the exception. |
printStackTrace() | It is used to print the stack trace of the exception. |
Example of SecurityException
In this example, we are trying to access a file that we do not have permission to access, so JVM will throw a SecurityException.
import java.io.File; public class SecurityExceptionExample { public static void main(String[] args) { File file = new File("test.txt"); // Check if the file exists and is readable if (file.exists() && file.canRead()) { System.out.println("File permission granted"); } else { System.out.println("SecurityException: access denied to read " + file.getAbsolutePath()); } } }
Output
Following is the output of the above code:
SecurityException: access denied to read /home/cg/root/27548/test.txt
As you can see in the output, JVM throws a SecurityException because we are trying to access a file that we do not have permission to access.
Handling SecurityException
In this example, we are trying to access a file that we do not have permission to access, so JVM will throw a SecurityException. We are handling this exception using try-catch block.
import java.nio.file.*; public class SecurityExceptionHandling { public static void main(String[] args) { Path path = Paths.get("test.txt"); try { // Check if the file exists and is readable if (Files.exists(path) && Files.isReadable(path)) { System.out.println("File access granted."); } else { System.out.println("Access denied: Cannot read file."); } } catch (SecurityException e) { // Handle general security issues that might occur in the file access System.out.println("SecurityException: " + e.getMessage()); } } }
Output
Following is the output of the above code:
Access denied: Cannot read file.
As you can see in the output, JVM throws a SecurityException because we are trying to access a file that we do not have permission to access.
How to avoid SecurityException?
To avoid a SecurityException, you should always check the permission of the file before accessing it.
Let's see an example to avoid SecurityException:
import java.io.File; import java.io.IOException; import java.nio.file.*; import java.nio.file.attribute.*; import java.util.Set; public class SecurityExceptionExample { public static void main(String[] args) { try { // Create a Path object for "test.txt" Path path = Paths.get("test.txt"); // Get the file permissions using Files PosixFileAttributes attributes = Files.readAttributes(path, PosixFileAttributes.class); Setpermissions = attributes.permissions(); // Check if the file has read permission if (permissions.contains(PosixFilePermission.OWNER_READ)) { System.out.println("File permission granted"); } else { System.out.println("File permission denied"); } } catch (IOException e) { System.out.println("IOException: " + e.getMessage()); } } }
Output
Following is the output of the above code:
IOException: test.txt
As you can see in the output, we are checking the permission of the file before accessing it.
By following the above ways, you can avoid the SecurityException in Java.