Exception Handling In C++

Exceptions In C++

Exceptions are best a way of handles or avoid runtime errors. If we traced any unhandled values we can transfer control to catch block for avoid runtime errors.To catch block we should place an end of code under try block. We can declare multiple catches for handle different type exceptions.Try block reached throw keyword, It passes control immediately to matched catch block.

try block:

Try block has the main code, which code may throw an exception. 

a try block syntax looks like the following:

try {
  //main code
}

Catch block:

The catch block contains exception code that is executed when throw (using throw keyword) by the Try Block.Possible to declare one or more catch block for different type of exception.No code can be between try block and the first catch block.Catch blocks declare directly after the try block. 

a catch block syntax looks like the following:

try {

} catch (ExceptionType1 name) {
 //exception code
} catch (ExceptionType2 name) {
 //exception code
}

throw Statement:

Using of throw statement, we can be thrown anywhere in the try block.throw statements triggered/execute a matched type of the catch block.The try block exists When a throw statement is reached.

a throw statement syntax looks like the following:

try {
   throw data_value;
} catch (ExceptionType1 name) {
 //exception code
} catch (ExceptionType2 name) {
 //exception code
}

Simple Exception Program In C++

// Simple Exception Program In C++
#include <iostream>

using namespace std;

int main() {
    //try block
    try {
        // Exception throwed
        throw 100;
    }    //catch block
    catch (int exception_value) {
        cout << "Exception Occurred : Exception Value : " << exception_value;
    }
    return 0;
}

Exception Occurred : Exception Value : 100

Multiple Catch Block Exception Program In C++

// Multiple Catch Exception Example Program (Divide By Zero ) In C++
#include <iostream>

using namespace std;

float divide(int x, int y) {
    if (y == 0) {
        throw y;
    } else if (y < 0) {
        throw "Negative Input";
    }
    return (x / y);
}

int main() {
    int i, result;
    //try block

    cout << "Enter the Number :";
    cin>>i;
    try {
        result = divide(100, i);
        cout << result << endl;
    }    //catch block
    catch (int exception_value) {
        cout << "Exception Occurred : Exception Value : " << exception_value;
    } catch (const char* excpection_str) {
        cout << "Exception Occurred : Exception Value : " << excpection_str;
    }
    return 0;
}

/*
Execution Scenarios
// Scenario 1
result = divide(100, 10);
cout << result << endl;
    
// Scenario 2
result = divide(100, 0);
cout << result << endl;
    
// Scenario 3
result = divide(100, -1);
cout << result << endl;
 */

Sample Output:

Scenario 1:
Enter the Number :10
10

Scenario 2:
Enter the Number :0
Exception Occurred : Exception Value: 0

Scenario 3:
Enter the Number :-1
Exception Occurred : Exception Value: Negative Input