An issue occurring during the execution of a programme is an exception (or extraordinary event). The usual flow of the programme is interrupted when an exception occurs and the programme / application terminates abnormally, which is not recommended, so these exceptions must be treated.
For several distinct reasons, an exception may occur. Any cases where an exception exists are below.
The invalid data was entered by a customer.
You can not locate a file which needs to be opened.
In the middle of communications, a network link has been broken or the JVM has run out of memory.
Some of these exceptions are due to user error, others are due to programmer error, and some are due to physical resources that have somehow failed.
We have three exemption types based on these. To know how exception handling functions in Java, you need to understand them.
Checked exceptions- A checked exception is an exception that is checked (notified) at compilation time by the compiler, which are often called exceptions for compile time. It is not possible to simply ignore these exceptions, the programmer should take care (handle) these exceptions.
Unchecked exceptions- An unchecked exception is an exception at the time of execution that happens. These are called Runtime Exceptions, too. These involve bugs in programming, such as logic errors or incorrect API use. At the time of the compilation, runtime exceptions are overlooked.
Errors- These are not exceptions at all, but issues that exist outside the user or programmer 's control. In your code, errors are usually overlooked because you can seldom do anything about an error. For example, an error will arise if a stack overload occurs. At the time of the compilation, they are still overlooked.
Hierarchy of Exception
The subtypes of the java.lang. Exception class are all exception classes. The exception class is a subclass of the class Throwable. There is another subclass called Mistake, rather than the exception class, that is derived from the Throwable class.
Errors are irregular situations which are not treated by the Java programmes in the event of serious failures. Errors are generated to show the errors the runtime environment produces. Example: The JVM is out of storage.
Following is the list of important methods available in the Throwable class.
1. public String getMessage()
Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor.
2. public Throwable getCause()
Returns the cause of the exception as represented by a Throwable object.
3. public String toString()
Returns the name of the class concatenated with the result of getMessage().
4. public void printStackTrace()
Prints the result of toString() along with the stack trace to System.err, the error output stream.
5. public StackTraceElement  getStackTrace()
Returns an array containing each element on the stack trace. The element at index 0 represents the top of the call stack, and the last element in the array represents the method at the bottom of the call stack.
6. public Throwable fillInStackTrace()
Fills the stack trace of this Throwable object with the current stack trace, adding to any previous information in the stack trace.