Categories

Jun 15 in C++

Q:

What is Exception Handling? Does C++ support Exception Handling?

1 Answer

Jun 16

Yes C++ supports exception handling.

We cannot ensure that code will execute normally at all times. There can be certain situations that might force the code written by us to malfunction, even though it’s error-free. This malfunctioning of code is called Exception.

When an exception has occurred, the compiler has to throw it so that we know an exception has occurred. When an exception has been thrown, the compiler has to ensure that it is handled properly, so that the program flow continues or terminates properly. This is called the handling of an exception.

Thus in C++, we have three keywords i.e. try, throw and catch which are in exception handling.

The general syntax for exception block is:

try{

 …. 

 # Code that is potentially about to throw exception goes here

 ….

 throw exception;

 }

 catch(exception type) {

 …

 #code to handle exception goes here

 }

As shown above, the code that might potentially malfunction is put under the try block. When code malfunctions, an exception is thrown. This exception is then caught under the catch block and is handled i.e. appropriate action is taken.

Click here to read more about C++
Click here to read more about Insurance

Related questions

Madanswer
Jun 15 in C++
Jun 11 in C++
Jul 16, 2019 in C++
...