Types Of Exceptions In Programming Languages


We do perform different actions using methods, functions in programming. But there might be some situations where our method shows some abnormal behavior. Let us take an example of a situation where we divide One by 0, there is no error in this statement, but the exception is that the division is not possible as it gives undefined value. But, instead of leaving the error, we need to inform the person coding that there is an unknown behavior for which we use Exceptions.

For abnormal condition occurrence to be indicated to a calling method in Java, we use as customary way called Exceptions. Today let us discuss about the different types of exceptions and how to use them. You will get to know more about the working behavior and nature of the Exceptions in programming Languages.

The exception is thrown when there is a result of the method that can’t be handled by itself with its abnormal behavior and needs more assistance by the coder. To explain it in more detail, it is just like throwing a red beep that there is a problem which can’t be handled automatically.

So, we would always want that the problem is automatically caught and solved. To catch these Exceptions, there are handlers which are positioned along the method of the thread invocation stack. If to catch the exception, there is no calling method; then an exception is thrown.

If any of the threads don’t catch the exception in the method of the invocation stack for a long time, then that thread will be expired. If you want your program to recover from the exceptions, it is needed to position the exception handlers which are catchers carefully and strategically in your program so that all the problems are handled.

Exception classes

Exceptions are nothing but objects in Java. An object is thrown when you throw an exception. Only the objects that are descended from the Throwable class can be thrown as an exception. In the Java.Lang library, for entire classes family, Throwable is the base class, which can be instantiated and thrown by your program.

Throwing exceptions

A simple keyword called Throw is used with the reference of an object for the exception to be thrown.

throw new ColdException();

The reference used must be either the Throwable class or the subclasses of it.

Here in the following code, let us take a class as a customer who ordered a coffee. The exceptions of it are an abnormal condition that is the coffee is to cold. As a throw keyword is used to throw a single exception, we use throws keyword in Java to throw more than one exceptions.

Catching exceptions

The exception that is thrown need to be caught. Try block which is associated with one or more catch clauses is used in Java to catch an Exception. One exception type is prepared to be handled by each catch clause in the code of a given program.


Related Articles