Exception handling is a process by which an unwanted error can be captured with the help of various statements. An exception can break the normal flow of the code execution and raises some unwanted error in the code or may crash an application, so to avoid these we need to handle these exceptions.

Exception handling in hardware

In hardware, exception handling mechanism is provided by the CPU. The exceptions occurred in the hardware are captured and handled by various service routines and the state before the exception occurred is captured and saved in the stack.

Exception handling in software

In software, the exceptions are captured differently than in hardware but the same concepts are involved in handling the exceptions. The exceptions can be thrown intentionally using throw keyword and these exceptions are captured by the catch block. The code which is expected to cause exception is placed within the try block and the exceptions are captured in the catch block.

  • Exceptions may cause some serious issues in an application which need to be handled carefully.
  • There are various types of exceptions such as NullPointerException, ArrayIndexOutOfBounds, ArithematicException, etc. These exceptions belong to a class Exception.
  • If the exceptions are thrown and are not captured, then these exceptions are caught at the runtime by the uncaught exception handler. The most common behaviour is that the handler will stop the execution and raises an error.
  • Uncaught exceptions in multi-thread operations are caught only by the top level handler. It will stop the thread in which the exception occurs, the entire process is not stopped.
  • There are various techniques used for handling these exceptions. The techniques vary from language to language. 

In java following code can be used-

try{

 //code which can cause exception
int a = 9 , b = 0;
int c;
c = a/b;
system.out.print(c);
}catch(Exception e){
e.printStackTrace(e)
}

In python, this code is used –

try:

int a = 9 , b = 0;
int c;
c = a/b;
print “c”
except ArithematicError:
 print "Unable to print"


Leave Comment