Java Exception
EXCEPTION IN JAVA
· An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. When an exceptional event occurs, an exception is thrown, which is a Java object that describes the error that occurred. The exception can be handled by the code, which can take appropriate action to resolve the error, such as displaying an error message to the user or attempting to recover from the error and continue execution.
· Exceptions can be thrown by the Java runtime system or by the application code, and they can be caught and handled by the application code. For example, if a program attempts to access a file that does not exist, a FileNotFoundException will be thrown. The code that is executing the file access operation can catch this exception and display an error message to the user, or it can pass the exception up the call stack to be handled by a higher level of the application.
· It is also possible to specify a finally block after a try-catch block, which will be executed whether or not an exception is thrown. This can be useful for cleaning up resources, such as closing files or releasing database connections, that were used in the try block.
· In summary, an exception in Java is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Exceptions can be thrown by the runtime system or by the application code, and they can be caught and handled by the application code using try-catch blocks.
Example :
try {
// code that may throw an exception
} catch (ExceptionType ex) {
// code to handle the exception
}
Exception Types image while searching from #chatGPT :
Exception Types:
According to Oracle, there are three types of exceptions in Java: checked exceptions, unchecked exceptions, and errors.Example of CheckedException in Java :
A checked exception is a type of exception that is checked by the compiler at compile time. This means that if a method throws a checked exception, the code that calls the method must either handle the exception using a try-catch block or declare that it throws the exception using a throws clause. Here is an example of a method that throws a checked exception:
Example :
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
File file = new File("myfile.txt");
try {
FileInputStream fis = new FileInputStream(file);
// read data from the file
fis.close();
} catch (IOException ex) {
// handle the IOException
}
}
}
Recommended by LinkedIn
public static void main(String[] args) throws IOException {
// code that may throw an IOException
}
Example of UncheckedException in Java :
Unchecked exceptions are typically runtime exceptions or errors that are not easily anticipated or recoverable. Examples of unchecked exceptions in Java include the NullPointerException, which is thrown when a program attempts to access a null object reference, and the ArrayIndexOutOfBoundsException, which is thrown when a program attempts to access an array element using an index that is outside the bounds of the array.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileReadExample {
public static void main(String[] args) {
File file = new File("myfile.txt");
try {
FileInputStream fis = new FileInputStream(file);
// read data from the file
fis.close();
} catch (IOException ex) {
// handle the IOException
}
}
}
Example of Exception type Error:
An error in Java is a severe issue that typically cannot be recovered from, and it typically indicates a problem with the Java virtual machine (JVM) or the underlying system rather than the application code. Examples of errors in Java include the StackOverflowError, which is thrown when the JVM runs out of memory on the stack, and the NoClassDefFoundError, which is thrown when a class that the application is trying to use cannot be found.
Here is an example of an error in Java: the OutOfMemoryError that is thrown when the JVM runs out of memory.
Example :
public class OutOfMemoryExample {
public static void main(String[] args) {
try {
// create a very large array that will cause an OutOfMemoryError
int[] largeArray = new int[Integer.MAX_VALUE];
} catch (OutOfMemoryError ex) {
// handle the OutOfMemoryError
}
}
}
All credit goes to OpenAI’s chatGPT .
Senior Technical Lead Java 2x AWS Certified
1yMeaningful article
QA Automation Engineer| Selenium| Rest Assured API
1yNice article Gayatri Mishra
Teaching testers how to code and automate
1yGood article on Java Exceptions Gayatri Mishra