Java | Exception Handling

05 min read

Exception handling:

Error:

  • Any departure from the expected behavior of the system or program, which stops the working of the system and cannot be handled at the application level but only at the system level.

  • Errors are always unchecked. Example: Out of Memory' error, 'Stack overflow', which can only be handled by the OS

  • Errors cannot be thrown, caught or created. Even if an 'OutofMemory' error is caught the user will not be in a position to fix it, as it depends on OS, architecture and server configuration.

Exception:

  • Any error or problem which a programmer or an application can handle and continue to work normally.
  • Exceptions may be checked or unchecked exceptions.
  • Exceptions can be created manually by the programmer, thrown or caught.

 

Difference between Checked and Unchecked Exceptions :

Checked Exceptions:

  • Exceptions which should either be handled or registered that these may be thrown from the method under consideration so that the caller of this method can handle these exceptions. Otherwise, the program will not compile.

  • Also called a compile-time exception.

  • Example: IOException, FileNotFoundException, etc. These exceptions should be handled or forwarded to the caller. Else, the compilation error will be generated.

Unchecked Exceptions:

  • The compiler ignores these exceptions and hence there will not be any compilation error, even if these exceptions are not handled or if these exceptions are not intimated to the caller of the method in which these exceptions arise.
  • Also called runtime exception.
  • Example: ArrayIndexOutOfBoundsException, ArithmeticException, etc. These exceptions do not stop the program from being compiled successfully even if they are not properly handled.

 

Exception handling in Java is managed through 5 keywords:-   

  • try
  • catch
  • throw
  • throws
  • finally

 

Exception:

It is an object that is created when an abnormal situation arises in the program. It does not always indicate an error but also can signal some particularly unusual event in the program that deserves special attention.

Advantages of Exception Handling:

  • Allows the programmer to fix the error and thus prevents the program from automatically terminating. Once the exceptions are handled, the program can resume execution from then onwards.
  • It separates the code that deals with errors from the code needed for the program logic. I knee, it increases readability. In languages like C, error handling code forms a part of the program logic, which reduces the program readability.

(eg) if( fp!=null)

{

......

if(! feof(fp))

......

else

.....

}

else

.....

Note: Only unusual or catastrophic situations should be signaled by exceptions and not all errors, as it involves a lot of processing overhead.

  1. try — Program statements that need to be monitored for exceptions are contained within a try block. An exception occurring within the try block is thrown.
  2. catch — Encloses the code that is needed to handle exceptions of a particular type that may be thrown in a try block.
  3. throw — Helps to manually throw either a predefined exception or user-defined exception (apart from the system generated exceptions).
  4. throws — If a method is capable of causing an exception that it does not handle, 'throws' clause is used to specify this behavior so that the callers of this method can guard themselves against that exception.
  5. finally — Code in this block is always executed before the method ends, regardless of whether any exceptions are thrown in the try block or not.
POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error