0
7.3kviews
Write a short note on Exception Handling Mechanism.
1 Answer
1
215views
  • Exception is a condition that is caused by run-time error in the program.
  • Exceptions can be generated by the Java run-time system, or they can be manually generated by our code.
  • Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java language or the constraints of the Java execution environment.
  • The purpose of exception handling mechanism is to provide a means to detect and to report exceptional circumstances so that appropriate action can be taken. The mechanism suggests incorporation of separate error handling code that performs following tasks:
  • Find the problem
  • Inform about error has occurred
  • Receive the error information
  • Take the corrective action
  • Java’s exception handling mechanism provides every step to be implemented.
  • The default exception handler provided by the Java run-time system is useful for debugging.
  • It provides two benefits. First, it allows us to fix the errors. Second, it prevents the program from automatically terminating. In order to take these two benefits Java has provided the mechanism of try-catch blocks. Its general form is as below:

    try
    {
    // block of code to monitor for errors
    }
    catch (ExceptionType1 exOb)
    {
    // exception handler for ExceptionType1
    }
    catch (ExceptionType2 exOb)
    {
    // exception handler for ExceptionType2
    }
    //.......
    
  • Here, ExceptionType is the type of exception that has occurred and exOb.

  • The keyword try is used to preface a block of code that is likely to cause an error condition and throw an exception.
  • The catch block catches the exception thrown by the try block and handles it appropriately. The catch block is added immediately after the try block. Fig. 5.2 shows this exception handling mechanism.

enter image description here

Please log in to add an answer.