An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons. Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.
To understand how exception handling works in Java, you need to understand the three categories of exceptions:
Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.
Runtime exceptions: A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.
Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.
Go through these tutorials to gain more insight.
Related Tags
Tutorials
java.lang.NoSuchMethodError means code compiled against one version of a class runs with another version that lacks the called method. Learn to read the message, find the wrong jar with mvn dependency:tree, -verbose:class and javap, and fix the conflict with a BOM, an exclusion and the Enforcer dependencyConvergence rule.
A StackOverflowError means a thread made more nested calls than its stack can hold. Learn to read the repeating stack trace, fix missing base cases, deep recursion and object cycles in toString(), Lombok and Jackson, and when -Xss helps.
IllegalArgumentException is the unchecked exception a Java method throws when an argument has a value it does not accept. This post lists the JDK calls that throw it with their real Java 25 messages and the fix for each, the subclasses such as NumberFormatException, and when our own code should throw it instead of IllegalStateException or NullPointerException.
“Unsupported class file major version 69” means a JVM or bytecode tool older than Java 25 tried to read a Java 25 class file. Learn to read the class file version, map it to the Java release, and fix the error by upgrading the runtime, compiling with –release, or upgrading Gradle, Spring, JaCoCo, Mockito and Lombok.
The error “java.lang.IllegalArgumentException: Failed to create folder” in Selenide typically happens when Selenide tries to save screenshots, page sources, or logs, but it cannot create the required directories due to permission issues or incorrect paths.
Learn to fix the selenium WebDriver error “This version of ChromeDriver only supports Chrome version” during UI testing.
The OutOfMemoryError is a common issue in Java applications, occurring when the JVM cannot allocate enough memory to fulfill a request.
Learn to solve the Java compiler exception “Java class file has wrong version” while migrating an application to Spring 6 or Spring boot 3.
The JMH error “Unable to find the resource: /META-INF/BenchmarkList” is mostly observed when we do not configure the compiler plugin.
In Java exception handling, throw keyword is used to explicitly throw an exception from a method or constructor. And throws keyword is used to declare the list of exceptions that may be thrown by that method or constructor. 1. Throw Let us learn basic things about throw keyword before going …
Java try catch finally blocks helps in writing the application code which may throw exceptions in runtime and gives us chance to recover from the exception.
You may get InvalidKeyException: Parameters missing error while performing AES encryption or decryption for password or any senstive information.
SSLHandshakeException means the Java client and the server could not agree on a TLS connection. Fix TLS version mismatches with jdk.tls.client.protocols, untrusted certificates with keytool, and find the rest with javax.net.debug.
To triage SSL issues, enable SSL debug logging by passing javax.net.debug=ssl runtime argument to your server/program.
I encountered this exception while building spring MVC file upload example. The error stacktrace looks like this: Solution You have incorrect version of apache commons-fileupload. To solve this error, you correct version 1.2.1. This will solve your error. Happy Learning !!
1. Problem If you are facing this error after importing an existing maven project into your eclipse IDE, then it means that http-servlet is not available in the project’s classpath and you must include it. The error looks like this. 2. Solution To include http-servlet into your classpath, you have …
You will get this exception message when you are trying to use named groups in java regex validation or matching. Root Cause of No match found Error in Regex Named Group Matching Error log will show the message like below: Solution You are probably not using “matcher.matches()” before fetching the …
If you are working on a web application and you define the taglibs in your web.xml file then you may encounter this exception. The exception trace in server console looks like this when you start up the server. Solution You just need to confirm the allowed tags by web.xml confirming …
Learn the difference between checked vs unchecked exceptions in Java, with simple explanations and examples. Learn Java exception handling best practices.
Learn to handle the exceptions thrown from the instance or static initializer blocks in Java by declaring them in constructors.
Reason When you are working with java libraries that internally depend on cglib, then it’s very normal to encounter the below error: This error is due to an incompatible version of cglib with additional jar files in your project, such as asm. Solution The solution is very easy. Just place …
A Java NullPointerException means code used a null reference as an object. Read the helpful NPE message, learn the common causes, and avoid NPEs with requireNonNull, Optional, default values, case null and JSpecify annotations.
Suppressed exceptions, as name suggest, are exceptions thrown in the code but were ignored somehow. If you remember try-catch-finally block execution sequence and how they return any value or exceptions, you will recall that exceptions thrown in finally block are suppressed if an exception is thrown in try block also. …
In this Java tutorial, learn about asynchronous and synchronous exceptions in Java. Also, learn how they are different with checked and unchecked exceptions. 1. Asynchronous and synchronous exceptions Normally Java differentiates the exceptions into two categories on basis of “timing” when they are discovered. These categories are checked and unchecked …
In contrast to traditional custom exceptions, the new approach uses static inner classes for every new exceptional scenario.