c_-_c_exception_handling:throwing_exceptions
This is an old revision of the document!
C - C++ Exception Handling - Throwing Exceptions
#include <iostream> #include <stdexcept> using std::cin; using std::cout; using std::endl; using std::runtime_error; class DivideByZeroException : public runtime_error { public: DivideByZeroException::DivideByZeroException(): runtime_error( "attempted to divide by zero" ) {} }; double quotient( int numerator, int denominator ) { throw DivideByZeroException(); // terminate function return 0; } int main() { try { double result = quotient( 1, 1 ); cout << "The quotient is: " << result << endl; } catch ( DivideByZeroException ÷ByZeroException ) { cout << "Exception occurred: " << divideByZeroException.what() << endl; } return 0; }
c_-_c_exception_handling/throwing_exceptions.1507804712.txt.gz · Last modified: 2020/07/15 09:30 (external edit)