0% found this document useful (0 votes)
48 views13 pages

Exception Handling

The document discusses exception handling in C++. It explains that exceptions are errors that occur during runtime and exception handling is the process of handling these errors. It describes how C++ provides features like try, catch, and throw for exception handling and discusses standard exception classes and exception handling in classes.

Uploaded by

Zen Looper
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)
48 views13 pages

Exception Handling

The document discusses exception handling in C++. It explains that exceptions are errors that occur during runtime and exception handling is the process of handling these errors. It describes how C++ provides features like try, catch, and throw for exception handling and discusses standard exception classes and exception handling in classes.

Uploaded by

Zen Looper
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/ 13

Object Oriented

Programming
Exception Handling
Exception Handling
• Exceptions are errors that occur at runtime.
• They are caused by a wide variety of exceptional circumstance, such
as running out of memory, not being able to open a file, trying to
initialize an object to an impossible value.
• The process of handling these exceptions is called exception handling.
• Using the exception handling mechanism, the control from one part
of the program where the exception occurred can be transferred to
another part of the code.
• Using exception handling in C++, we can handle the exceptions so that
our program keeps running.
Exception Handling
• 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.

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
}
#include <iostream>
#include <stdexcept>
using namespace std;
Output
Exception Division by zero not allowed!
int main()
{

// try block
try {
int numerator = 10;
int denominator = 0;
int res;

// check if denominator is 0 then throw runtime


// error.
if (denominator == 0) {
throw runtime_error(
"Division by zero not allowed!");
}

// calculate result if no exception occurs


res = numerator / denominator;
//[printing result after division
cout << "Result after division: " << res << endl;
}
// catch block to catch the thrown exception
catch (const exception& e) {
// print the exception
cout << "Exception " << e.what() << endl;
}

return 0;
}
#include <iostream>
using namespace std;
Output
int main() Before try Inside try Exception Caught After catch
{ (Will be executed)
int x = -1;

// Some code
cout << "Before try \n";

// try block
try {
cout << "Inside try \n";
if (x < 0) {
// throwing an exception
throw x;
cout << "After throw (Never executed) \n";
}
}

// catch block
catch (int x) {
cout << "Exception Caught \n";
}

cout << "After catch (Will be executed) \n";


return 0;
}
Special Catch block
• There is a special catch block called the ‘catch-all’ block, written as catch(…), that
can be used to catch all types of exceptions.
#include <iostream>
using namespace std;

int main()
{
// try block
Output
try { Default Exception

// throw
throw 10;
}

// catch block
catch (char* excp) {
cout << "Caught " << excp;
}

// catch all
catch (...) {
cout << "Default Exception\n";
}
return 0;
}
C++ Standard Exceptions
Standard Exception classes
• Standard Exception Classes:
• C++ provides several standard exception classes, such as
std::runtime_error, std::logic_error, std::invalid_argument, etc., which
can be used to represent different types of errors.
Standard Exception classes
Practice Question
• Write a C++ function named calculateSumOfSquares that takes three parameters of type int. The function
should calculate the sum of squares of all the parameters if the following conditions are met:
• All parameters are different.
• All parameters are positive.
• All parameters are odd.
If any of these conditions are not met, the function should throw different exceptions:
• If the parameters are not different, throw an exception with the message "Parameters must be different".
• If any parameter is not positive, throw an exception with the message "Parameters must be positive".
• If any parameter is not odd, throw an exception with the message "Parameters must be odd".
In the main function:
• Prompt the user to input three integers.
• Call the calculateSumOfSquares function with the user-provided integers as arguments.
• Handle any exceptions thrown by the function and display appropriate error messages.
• If no exceptions are thrown, display the calculated sum of squares.
Exception Handling in classes
Exception Handling in classes
• Consider Book chapter#14 example for Practice.

You might also like