User Tools

Site Tools


c_-_c_exception_handling:throwing_exceptions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
c_-_c_exception_handling:throwing_exceptions [2017/10/12 10:37] – created peterc_-_c_exception_handling:throwing_exceptions [2020/07/15 09:30] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== C - C++ Exception Handling - Throwing Exceptions ====== ====== C - C++ Exception Handling - Throwing Exceptions ======
 +
 +<code cpp>
 +#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 &divideByZeroException )
 +  {
 +    cout << "Exception occurred: " << divideByZeroException.what() << endl;
 +  }
 + 
 +  return 0;
 +}
 +</code>
 +
  
c_-_c_exception_handling/throwing_exceptions.1507804673.txt.gz · Last modified: 2020/07/15 09:30 (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki