DEV Community

Piyush
Piyush

Posted on

Exceptions "against" Errors

Common misconception during programming

In java, both 'Exceptions' and 'Errors' are subclasses of "java.lang.throwable" class.
Error refers to an illegal operation performed by the user which results in abnormal behavior of the program. Programming errors many a time remain undetected until the program is executed or compiled. Some errors inhibit the program from getting executed. Thus making it necessary to remove errors before compiling or executing. Errors are of three types:

  • Compile-time errors
  • Run-time errors
  • Logical errors

Compile-time errors
Compile-time errors are those errors which prevent the code from running because of an incorrect syntax such as a missing semicolon at the end of a statement or a missing bracket, etc. These errors are detected by the java compiler and an error message is displayed onto the screen while compiling. Compile-time errors are also referred to as "Syntax errors".

Image description

Image description

Run-time errors
Run-time errors occur during the execution of the program. Sometimes these are discovered when the user enters an invalid data or data which is not relevant. Run-time errors occur when a program does not contain any syntax errors but asks the computer to do something that the computer is unable to reliably do. The compiler has no technique to detect these kind of errors. It is the JVM (Java Virtual Machine) which detects it while the program is running.

Image description

Logical errors
When the program compiles and executes, but does the wrong thing or returns an incorrect result or shows no output; it is a logical error. These errors are neither detected by compiler nor by JVM. The system has no idea what the program is supposed to do, so it provides an additional information to help find the error. Logical errors are also called Semantic errors.

Image description

Image description

Exceptions in java refer to an unwanted event, which occurs during the execution of a program, i.e. at run-time, that disrupts the normal flow of the program's instructions. Exceptions are the conditions that occur at runtime and may cause the termination of the program. But they are recoverable using try, catch and throw keywords. Exceptions are divided into two categories:

  • Checked exceptions
  • Un-checked exceptions

Checked exceptions are like IOException known to the compiler at compile time .
Un-checked exceptions are like ArrayIndexOutOfBoundException known to the compiler at runtime, mostly caused by the program written by programmer.

Image description

Top comments (1)

Collapse
 
scottshipp profile image
scottshipp • Edited

Nice article! It's important to point out that the Error class in Java is not used in practice. It is for special cases described by its JavaDoc as follows:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

Since, by definition, it should not be caught, it is almost never found in Java code.

On a separate note, the Java Trail article "Unchecked Exceptions—The Controversy" is a great reference to bookmark. This article presents the following guideline to help Java programmers determine when to use checked or unchecked exceptions in a standard, shared way:

Here's the bottom line guideline: If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception.

You'll find that most third-party libraries in the Java ecosystem adhere to this idiom.