top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Exception Handling using C++

0 votes
339 views

Introduction to Exceptions
C++ provides a built in mechanism for handling errors known as exception handling. Run-time errors can be easily managed and handled using exception handling feature. An exception can be called as an error or an unexpected event. Exceptions can occur due to the following reasons.

Synchronous events (errors): These are the errors that occur in the natural flow of a program, like a divide by zero error. Errors that occur due the execution of the programs fall into this category.

Asynchronous events: These occur randomly like a mouse click, or a disk error. Errors that do not occur due to execution of programs fall into this category.

The Need for Exception Handling 

Code written to handle exception is known as exception handler. The need for an exception handler arises because exceptions might cause unpredicted program termination. They may also result in abnormal behavior of the program. For example, consider a situation wherein data is fed to a program and the program performs a division operation on the data. If the user enters some data, which is accidentally divided by zero within the program, the process of reentering the data should begin again. At times, some critical errors may result in hardware problems. Although simple C++ statements can be used to check, errors but these statements become redundant throughout the program. This may consume space and time. Such problems can be avoided by the use of exception handling.

Handling Exceptions

The approach, which must be followed, in order to take care of exceptions is an follows:

  • Monitor the code, which is likely to generate exceptions.
  • On detection of error, pass the information to take corrective measures.
  • Take the remedial actions.

C++ provides three keywords, which are used in exception handling. These keywords are listed:-

  • Try
  • Throw
  • Catch

The keyword try is used to monitor for exception. The keyword throw is used pass the error information in the form of parameters. The keyword catch encloses the code, which performs remedial action.

Syntax:

try {
  try {
      // code here
  }
  catch (int n) {
      throw;
  }
}
catch (...) {
  cout << "Exception occurred";
}
 

For More Go through This Video:-

posted Jul 28, 2017 by Sulekha Kumari

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Causes of Exceptions

An exception is an abnormal condition that arises out of an extraordinary situation disrupting the flow of program’s instructions. Exceptions report error conditions in a program.

                                    

In programs, exceptions can occur due to any of the following reasons:

  • Programming errors: These exceptions arise due to errors present in APIs, such as NullPointerException. The program using these APIs cannot do anything about these errors.
  • Client code errors: The client code attempts operations, such as reading content from a file without opening it. This will raise an exception. The exception will provide information about the cause of the error and is handled by the client code.
  • Errors beyond the control of a program: There are certain exceptions that are not expected to be caught by the client code such as memory error or network connection failure error. These are errors which have been raised by the run-time environment.

                                                                                      

Classification of Exceptions

When an error occurs in a method during runtime, an object is created containing information about the error, its type and the state of the program. This object is referred to as exception object. Exception objects are instances of classes that are derived from the base class Throwable.

In Java there are two types of exceptions. They are:

  • Checked Exceptions: These types of exceptions are derived from the Exception class. The program either handles the checked exception or moves the exception further up the stack. It is checked during compilation.
  • Unchecked Exception: These exceptions are derived from the RuntimeException class, Which in turn is derived from the Exception class. The program need not handle unchecked exception. It is generated during the execution of the program.

 

Types of Checked Exceptions

All checked exceptions are derived from the Exception class. Some of the common checked exceptions are:-

InstantiationException: This exception occurs if an attempt is made to create an instance of the abstract class.

InterruptedException: This exception occurs if a thread is interrupted.

NoSuchMethodException: This exception occurs if the java virtual Machine is unable to resolve which method is to be called.

RuntimeException: This exception may be thrown during normal operation of java virtual machine if some erroneous condition occurs.

 

Types of Unchecked Exceptions

All Unchecked exceptions are directly or directly or indirectly derived from the RuntimeException class. Some of the common Unchecked exceptions are described:

ArithmeticException: Derived from RuntimeException and indicates an Arithmetic error condition.

ArrayIndexOutOfBoundsException: Derived from RuntimeException. Method receives an illegal arugument.

NegativeArraySizeException: Derived from RuntimeException. Array size is less than zero.

NullPointerException: Derived from RuntimeException. Attempt to access a null object member.

NumberFormatException: Derived from IllegalArgumentException. Unable to convert the string to a number.

StringIndexOutOfBoundsException: Derived from IndexOutOfBoundsException. Index is negative or greater than the size of the string.

READ MORE

What is Angular App Exception Handling?

The AngularJS $exceptionHandler service allows you to catch and handle unanticipated JavaScript errors in a meaningful way.

Example:
app.factory('$exceptionHandler',function($log,ErrorService) {
    return function(exception,cause) {
        if(console) {
        }
        
    }
    ErrorService.send(exception,cause);
});

Any uncaught exception in angular expressions is delegated to this service. The default implementation simply delegates to $log.error which logs it into the browser console. In unit tests, if angular-mocks.js is loaded, this service is overridden by mock $exceptionHandler which aids in testing.

$exceptionHandler is very useful for sending errors to third party error logging services or helpdesk applications. Errors trapped inside of event callbacks are not propagated to this handler, but can manually be relayed to this handler by calling $exceptionHandler(e) from within a try catch block.

Video Tutorial for handling Exception Handling

https://www.youtube.com/watch?v=9xdIvUD2Jug

READ MORE
...