0% found this document useful (0 votes)
186 views12 pages

Exceptions Handling - PPT

Exception handling in C++ uses try, catch, and throw keywords. Code that can throw exceptions is placed in a try block. When an exception occurs, program control transfers to the catch block, where the exception can be handled. The catch block matches the type of object thrown. Throw is used to explicitly throw an exception and pass a parameter to the catch handler. For example, a function to perform division checks for a zero denominator, and if encountered, throws a runtime_error exception that is caught and handled in the catch block.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views12 pages

Exceptions Handling - PPT

Exception handling in C++ uses try, catch, and throw keywords. Code that can throw exceptions is placed in a try block. When an exception occurs, program control transfers to the catch block, where the exception can be handled. The catch block matches the type of object thrown. Throw is used to explicitly throw an exception and pass a parameter to the catch handler. For example, a function to perform division checks for a zero denominator, and if encountered, throws a runtime_error exception that is caught and handled in the catch block.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Exception Handling in C++

Errors can be broadly categorized into two types.

Compile Time Errors


Run Time Errors

Compile Time Errors – Errors caught during compiled time is called Compile time errors.
Compile time errors include library reference, syntax error or incorrect class import.

Run Time Errors - They are also known as exceptions. An exception caught during run time
creates serious issues.

Exception handling is the process of handling errors and exceptions in such a way that they
do not hinder normal execution of the system.

For example, User divides a number by zero, this will compile successfully but an exception
or run time error will occur due to which our applications will be crashed.
In C++, Error handling is done using three keywords:

try
catch
throw

Syntax:

try
{
//code
throw parameter;
}
catch(exceptionname ex)
{
//code to handle exception
}
try block
The code which can throw any exception is kept inside(or enclosed in) a try block.

Then, when the code will lead to any error, that error/exception will get caught inside the catch
block.

catch block

catch block is intended to catch the error and handle the exception condition.

We can have multiple catch blocks to handle different types of exception and perform different
actions when the exceptions occur.

For example, we can display descriptive messages to explain why any particular excpetion
occured.

throw statement
It is used to throw exceptions to exception handler i.e. it is used to communicate information
about error.
A throw expression accepts one parameter and that parameter is passed to handler.
throw statement is used when we explicitly want an exception to occur, then we can use throw
statement to throw or generate that exception.
• If the try block throws an exception then program control leaves the block and
enters into the catch statement of the catch block.

• If the type of object thrown matches the argument type in the catch statement,
the catch block is executed for handling the exception.

Try Block

Detects and throws an


Exception

Catch Block

Catch and Handles


Exception
#include <iostream> int main()
#include <stdexcept> // To use runtime_error {
using namespace std; float numerator, denominator, result;
cout<<"enter the numerator";
cin>>numerator;
// Defining function Division cout<<"enter the denominator";
float Division(float num, float den) cin>>denominator;
{
// If denominator is Zero // try block calls the Division function
// throw runtime_error try {
result = Division(numerator, denominator);
if (den == 0) {
throw runtime_error("Math error: Attempted to // this will not print in this example
divide by Zero\n"); cout << "The quotient is "
} << result << endl;
}
// Otherwise return the result of division
return (num / den); // catch block catches exception thrown
// by the Division function
catch (runtime_error& e) {
} // end Division
// prints that exception has occurred
// calls the what function
// using runtime_error object
cout << "Exception occurred" << endl
<< e.what();
}

} // end main
Division by zero exception
Division by zero exception: An exception can be thrown from outside try block aslong as it is thrown by a function that is called from try block
Handling Derived Class Exceptions
Handling Derived Class Exceptions

You might also like