Java Errors and ExceptionsBoth Errors and Exceptions are the subclasses of java.lang.Throwable class. Errors should not be caught or handled (except in the rarest of cases). Exceptions are the bread and butter of exception handling. An Error "indicates serious problems that a reasonable application should not try to catch." While, an Exception "indicates conditions that a reasonable application might want to catch." Errors tend to signal the end of your application as you know it. It typically cannot be recovered from and should cause your Virtual Machine to exit. Catching them should not be done except to possibly log or display and appropriate message before exiting. Mostly Error is thrown by JVM in a scenario which is fatal and there is no way for the application program to recover from that error. For instance OutOfMemoryError .
There are three kinds of errors:
- Syntax Errors
- Runtime Errors
- Logic Errors
Syntax errorsSyntax errors are errors where the compiler finds something wrong with your Java program , and you can't even try to run it. For example, you may have incorrect punctuation, or may be trying to use a variable that hasn't been declared. These errors are the easiest to find and correct. The compiler will tell you where it got into problems, and its best guess as to what you did wrong.
Runtime errors occur when a program with no syntax errors asks the computer to do something that the computer is unable to reliably do . Common examples are:
- Trying to divide by a variable that contains a value of zero.
- Trying to open a file that doesn't exist.
Logic errorsLogic errors occur when a program does not perform the way it was intended to . The JVM, of course, has no idea what your program is supposed to do , so it provides no additional information to help you find the error. Common examples are:
- Multiplying when you should be dividing
- Adding when you should be subtracting
- Opening and using data from the wrong file
- Displaying the wrong message
In general, syntax errors are easy to find and easy to correct because the compiler gives indications as to where the errors came from and why they are wrong. Runtime errors are not difficult to find, either, since the reasons and locations for the errors are displayed on the console when the program aborts. Finding logic errors , on the other hand, can be very challenging.
- Reached end of file while parsing
- Unreachable statement error in Java
- Int Cannot Be Dereferenced Error - Java
- How to fix java.lang.classcastexception
- How to fix java.util.NoSuchElementException
- How to fix "illegal start of expression" error
- How to resolve java.net.SocketException: Connection reset
- Non-static variable cannot be referenced from a static context
- Error: Could not find or load main class in Java
- How to Handle OutOfMemoryError in Java
- How to resolve java.net.SocketTimeoutException
- How to handle the ArithmeticException in Java?