fork download
  1. #include <iostream>
  2. #include <exception>
  3. using namespace std;
  4.  
  5. namespace MathError {
  6. struct DivisionByZero : public std::exception {
  7. const char* what() const throw() {
  8. return "DivisionByZero";
  9. }
  10. };
  11. }
  12.  
  13. float division(float x, float y) {
  14. if(y == 0.0) {
  15. throw MathError::DivisionByZero();
  16. } else {
  17. return x/y;
  18. }
  19. }
  20.  
  21. int main() {
  22. try {
  23. cout << "Ratio: " << division(5.0, 0.0) << endl;
  24. } catch (MathError::DivisionByZero &e) {
  25. cout << "Error: " << e.what() << endl;
  26. }
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
Error: DivisionByZero