12

Exception Handling in C Plus Plus (C++)

Harsh Gupta - Tech Writer
In C++ after editing and compiling you will execute the program so that it will perform the task which you want to do with your system. While running the program, sometimes the program runs correctly but sometimes errors or bugs appear in the program. An error may produce incorrect output or it may terminate the execution of the program. Therefore, it is necessary to remove all type of errors in the program. The process of removing errors is called debugging and ways of removing errors as debugging techniques.

Mainly errors are classified into two categories:

Compile time error:

These are the error which occurs whenever you compile the program. We can't continue further until we remove the errors. Some compile time errors are:

· Missing semicolon

· Missing double quotes in string

· Use of undeclared variable

· Missing curly braces({ })

Runtime error:

With runtime error the compiler doesn't produce any error but result produce by program may be incorrect or some output is produced again and again without stopping or no output is produced. Different type of runtime errors are:

· Divide by zero

· Try to store or use an array that is out of bound

· Using an infinite loop without break statement

An exception is a condition that is caused by a runtime error in the program. If we want to execute the remaining code excluding the errors, then you should catch the exception object given by the error condition & then display an appropriate message. This process is known as exception handling.

Try-catch block:

The exception handling allows one section of a program to serve and dispatch error condition and another section to handle them. These two sections or blocks are represented by try and catch.

Syntax:

Refer to the picture for syntax!

Throw statement:-

To detect an exception and jump to a catch handler, a C++ function uses the throw statement with the data type that matches the parameter list of proper catch handlers.

Example: throw "An error has occurred".

If we want to divide two numbers and an error will occur when we divide a number by zero for this the program will be like this:

Refer to the picture for the program!

So the exception handling mechanism performs the following tasks:

· Hit the exception or Find the problem

· Throw the exception

· Catch that exception and,

· Handle the exception

If a program has more than one condition to throw an exception than more than one catch statement with try can be used.

Published by Harsh Gupta - Tech Writer

I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com  View profile

  • An exception is a condition that is caused by a runtime error in the program.
  • The process of removing errors is called debugging and ways of removing errors as debugging techniqu
The exception handling allows one section of a program to serve and dispatch error condition and another section to handle them. These two sections or blocks are represented by try and catch.

To comment, please sign in to your Yahoo! account, or sign up for a new account.