Difference between Error and Exception in Java7 May 2025 | 4 min read The general meaning of exception is a deliberate act of omission, while the meaning of error is an action that is inaccurate or incorrect. In Java, Exception and Error are both subclasses of the Java Throwable class that belongs to java.lang package. But there exist some significant differences between them. So, in this section, we are going to discuss the key differences between error and exception. ![]() Before moving ahead in this section, let's have a look at the hierarchy of the Java Throwable class. ![]() ExceptionThe term exception is shorthand for the phrase exception event. It is an event that occurs during the execution of the program and interrupts the normal flow of program instructions. These are the errors that occur at compile time and run time. It occurs in the code written by the developers. It can be recovered by using the try-catch block and the throws keyword. There are two types of exceptions, i.e. checked and unchecked. Some important points should be kept in mind while dealing with the exception:
Advantages of Exceptions
Let's understand the exception through a Java program. Example of ExceptionExampleLet's run the above program and enter a float value deliberately to generate an exception. ![]() It shows the InputMismatchException. Because the program accepts an integer value, we observe that the next statement is skipped and the program is terminated. ErrorErrors are problems that mainly occur due to the lack of system resources. It cannot be caught or handled. It indicates a serious problem. It occurs at run time. These are always unchecked. An example of errors is OutOfMemoryError, LinkageError, AssertionError, etc. are subclasses of the Error class. Let's understand the error through a Java program. Example of ErrorExampleCompile and RunOutput: ![]() When we execute the above program, we get the StackOverflowError, which is not an exception. Let's discuss the key differences between error and exception. Difference Between Error and ExceptionIn Java, Error and Exception are both subclasses of the Java Throwable class that belongs to java.lang package.
Next TopicHardy-ramanujan-theorem-in-java |
Greater Number with Same Set of Digits in Java A number (num) is given. The task is to find a number that is the smallest that comprises the same set of digits as num and must be larger than the number num. If the number num...
8 min read
In Java, there are many ways to swap two numbers. Generally, we use either swap() method of the Math class or use a third (temporary) variable to swap two numbers. Except these two ways, we can also swap two numbers using the bitwise operator (XOR) and...
3 min read
Java is not opening up is an error or troubleshooting that may arise due to certain reasons. But we can keep some points in mind so that time on installing Java and opening it, we may not get such issues. In this section, we will...
4 min read
Java, a widely-used programming language, offers the benefits of object-oriented programming. However, like other languages, Java has its share of challenges, one of them is the occurrence of duplicate class errors. The error can be perplexing for developers, especially those who are new to Java or...
5 min read
The 3N+1 problem is an abstract mathematical problem that is a conjecture (not yet proven). It is also known as Collatz problem. In this section, we will discuss the 3N+1 problem along with its Java program. The task is to write a Java program that will read...
3 min read
ASCII stands for American Standard Code for Information Interchange. ASCII is a standard data-transmission code that is used by the computer for representing both the textual data and control characters. ASCII is a 7-bit character set having 128 characters, i.e., from 0 to 127. ASCII represents...
12 min read
The railway station problem is the one of the most important problem usually asked in coding round interview to test the logic ability and problem resolving skill of the candidate. The Railway Station Problem In this problem, the arrival and departure time of trains are provided for a...
5 min read
The Triangle Inequality Theorem is used to check if three given edges can create a triangle. The theorem affirms that the total of two sides needs to exceed the third side. Using this rule, we can quickly verify if the edges can form a valid triangle,...
5 min read
The bully algorithm is a type of Election algorithm which is mainly used for choosing a coordinate. In a distributed system, we need some election algorithms such as bully and ring to get a coordinator that performs functions needed by other processes. Election algorithms select a single...
4 min read
The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can either attest to our expertise or assist us in learning the Java coding language. A Java...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India