Exception Handling
Exceptions are runtime errors or anomalies or unusual conditions that a
program encounters during its execution.
These anomalies might include conditions such as division by zero,
access to an array outside of its bound or running out of memory or disk
space.
The process of handling these exceptions is called exception handling.
Using the exception handling mechanism (try and catch block), the
control from one part of the program where the exception occurred can
be transferred to another part of the code.
Exception handling handles the exceptions so that our program keeps
running.
try and catch block
C++ provides an inbuilt feature for Exception Handling. It can be done using the
following specialized keywords: try, catch, and throw with each having a
different purpose.
Syntax of try-catch in C++
try {
// Code that might throw an exception
throw SomeExceptionType("Error message");
}
catch( ExceptionName e1 ) {
// catch block catches the exception that is thrown from
try block
}
1. try in C++
The try keyword represents a block of code that may throw an exception
placed inside the try block. It’s followed by one or more catch blocks. If an
exception occurs, try block throws that exception.
, 2. catch in C++
The catch statement represents a block of code that is executed when a
particular exception is thrown from the try block. The code to handle the
exception is written inside the catch block.
3. throw in C++
An exception in C++ can be thrown using the throw keyword. When a program
encounters a throw statement, then it immediately terminates the current
function and starts finding a matching catch block to handle the thrown
exception.
Note:
Multiple catch statements can be used to catch different type of
exceptions thrown by try block.
The try and catch keywords come in pairs: We use the try block to test
some code and If the code throws an exception we will handle it in our
catch block.
Example 1
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
// try block
try {
int a = 10;
int b = 0;
int result;
// check if denominator is 0 then throw runtime
// error.
if (b == 0) {
throw runtime_error("Division by zero not allowed!");
Exceptions are runtime errors or anomalies or unusual conditions that a
program encounters during its execution.
These anomalies might include conditions such as division by zero,
access to an array outside of its bound or running out of memory or disk
space.
The process of handling these exceptions is called exception handling.
Using the exception handling mechanism (try and catch block), the
control from one part of the program where the exception occurred can
be transferred to another part of the code.
Exception handling handles the exceptions so that our program keeps
running.
try and catch block
C++ provides an inbuilt feature for Exception Handling. It can be done using the
following specialized keywords: try, catch, and throw with each having a
different purpose.
Syntax of try-catch in C++
try {
// Code that might throw an exception
throw SomeExceptionType("Error message");
}
catch( ExceptionName e1 ) {
// catch block catches the exception that is thrown from
try block
}
1. try in C++
The try keyword represents a block of code that may throw an exception
placed inside the try block. It’s followed by one or more catch blocks. If an
exception occurs, try block throws that exception.
, 2. catch in C++
The catch statement represents a block of code that is executed when a
particular exception is thrown from the try block. The code to handle the
exception is written inside the catch block.
3. throw in C++
An exception in C++ can be thrown using the throw keyword. When a program
encounters a throw statement, then it immediately terminates the current
function and starts finding a matching catch block to handle the thrown
exception.
Note:
Multiple catch statements can be used to catch different type of
exceptions thrown by try block.
The try and catch keywords come in pairs: We use the try block to test
some code and If the code throws an exception we will handle it in our
catch block.
Example 1
#include<iostream>
#include<stdexcept>
using namespace std;
int main()
{
// try block
try {
int a = 10;
int b = 0;
int result;
// check if denominator is 0 then throw runtime
// error.
if (b == 0) {
throw runtime_error("Division by zero not allowed!");