Exception handling, Exception class, try-catch-finally and throw
The Three Types of Programming Errors aka Exceptions
Compile Time Errors
Logical Errors
Runtime Errors
These errors in Java programming language are called exceptions.
Exception Class
The class Exception refers to an exceptional event in Java.
It is an event that occurs during the execution of a program that disrupts the normal flow of instructions which could lead to unintended outcomes, generate error messages, or even an application crash.
The Exception class is a subclass of Throwable class and java.lang.Object as root of the class hierarchy. Only objects of Throwable class can be thrown and caught.
Exception Object
When an error occurs, an object called Exception Object is created and passed to the runtime system which contains information about the error.
Throwing an Exception
Creating and passing of the Exception Object is called throwing an exception.
Catching an Exception
Once the exception is thrown, the runtime system then tries to look for an appropriate handler for this event. The handler chosen is said to catch the exception.
Kinds of Exceptions
There are three kinds of exceptions in Java:
1. Checked exceptions
Checked exceptions are exceptional conditions that an application should anticipate and recover from, like reading a non-existing file, this should be handled and notify the user.
2. Error exceptions
Error exceptions are exceptional conditions that are external to the application and the application cannot anticipate or recover from such as a hardware malfunction.
3. Runtime exceptions
Runtime exceptions are exceptional conditions that are internal to the application and application cannot anticipate or recover from; an example is a logic error like passing null as an argument to method call that expects a non-null object.