Exceptions and Exception Handling

Exceptions are Unexpected Event That Occurs During Program Execution. It Affects The Flow Of Program Instructions Which Can Cause The Program To Terminate Abnormally.

“The whole world full of errors and warnings,

Be with someone who can handle your exceptions”.

Today the world is moving towards the high technology world. Companies are using more technologies for developing desirable products. There is one of the technologies Java which is in use by multinational software companies such as Edge, data-core system Inc., etc.. Java technology is mostly use by Java developers for software development. 

In the development of software, Java developers face problems of errors while they code for making modules of the software. Errors represent irrecoverable conditions such as Java virtual machine (JVM) running out of memory, infinite recursion, library incompatibility, etc. Errors are not control by the programmers, and we should not try to handle errors. For this problem, the concept of exception handling comes from Java. 

Although Java has been out there for almost 24 years, Java developers still face problems implementing the exception handling in the most proper way, to be reusable, encapsulated, centralized, and pluggable. In this article, we propose an implementation for exception handling towards making the exception handling more robust and efficient.

 for many reasons exceptions occurs:

  1. Code errors
  2. Loss of network connection
  3. Opening an unavailable file
  4. Device failure

Java Exceptions hierarchy

(https://www.canva.com/design/DAEMVQNopDM/lPZkpkqSWWY9BxnbGuBVqA/view?utm_content=DAEMVQNopDM&utm_campaign=designshare&utm_medium=link&utm_source=publishsharelink

In the above figure, The root class in the hierarchy is a Throw class and it is split into two branches: Exception and Error. 

Exceptions in Java

An exception is a condition that cause by a run-time error in the program. When the java interpreter is caught an error such as array index error, division by zero, the interpreter creates an exception object and throws it to inform that an error has occurred. A java exceptions is nothing but an object which provides the information of an exceptional condition that occurred in the program. When an exceptional condition occurs, an object which represents an exception is created.

The object must be thrown in a method that caused the error and this method can handle an exception itself or pass it on. An exception can be generated by the java run time system or by manually by the code. The exception handling mechanism provides a means to report “exceptional circumstances”  so that corrective action can be taken. The exception handling mechanism of a separate error handling code that performs the following task:

  1. Find the problem. 
  2. Inform that an error has occurred that is throw the exception. 
  3. Receives the error information that catches the exception. 
  4. Take corrective action that is Handle the exception. 

If an exception is create and throw but an exception object is not catch and handled properly. the java interpreter will display an error message, and sometimes it will terminate the program. 

The exception handling can manage by five keywords:-

  1. Try: The statement of the program that to be monitor on for the exception is contain within the try block. If in a try block an exception occurs, it is throw . 
  2. Catch:  The exception thrown by try block, it catches the exception using catch and can handle it in some rational manner. 
  3. Throw: The system generates an exception that is automatically throw on by the Java runtime system.
  4. Throws: Any exception generated out of a method must be specified as such by a ‘throws’ clause
  5. Finally: Any code that is expect before a method returns are put in a ” finally block”.

The idea of an exception handling mechanism is throwing an exception and catching it. 

The try keyword is in use for the block of codes that causes an error condition and throws an exception. Catch keyword is use for the block of code that ‘catches’  the exception “thrown”  by the try block and can handle it. The catch block is immediately insert upon after the try block. 

The general form of exception handling blocks is :

Figure: Syntax for try and block

 The try block contains one or more statements that generate an exception. If the one statement generates the exception, then the remaining statements are skip upon and execution jumps to catch block 1.

The catch block can also contain one or more statements that are necessary to process the exception. Every try statement must be followed by at least one catch statement otherwise compilation error occurs. If the catch parameter matches the type of object of exception, then the exception is caught and statements in the catch block will be executed. Otherwise, an exception is not caught and default exception handles will cause the execution to terminate. 

throws 

If a method is generating an exception but cannot handle it, then, it is the responsibility of the method to specify activities to a caller method to guard themselves against that exception. To do this includes a throws clause in the method’s declaration. 

If they are not generate, then the compile-time error will then occur.

The generated form of a method declaration that includes the throw clause is :

Figure: Syntax for throws in Java

Finally

The final statement is use to handle an exception that is not catch by any of the previous catch statements. 

Syntax of finally block :

Figure: Syntax of finally with try and catch block

When finally block define, it is definitely execute. 

Conclusion:

The module begins by introducing the exceptions and how they can be handled in java programming. How exceptions are give to show that a problem has occur in a program. From there, we go on to see how exceptions are catch and handle.

The module conclude it by taking a look at the final clause. however, it provides a means of guaranteeing the execution of a section of code regardless of whether an exception is given.

Written By: Vickky Bopche

Reviewed By: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *