Exception Handling
Exception Handling
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;
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";
}
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.