fork download
  1. #include <iostream>
  2. #include <cmath>
  3. #include <stdexcept>
  4.  
  5. using namespace std;
  6.  
  7. double my_sqrt(double x)
  8. {
  9. if(x < 0)
  10. {
  11. throw invalid_argument("sqrt received negative argument");
  12. }
  13. return sqrt(x);
  14. }
  15.  
  16.  
  17. int main(int argc, char *argv[])
  18. {
  19. try
  20. {
  21. double s = my_sqrt(-1);
  22. }
  23. catch(const exception& e)
  24. {
  25. cout << "Caught " << e.what() << endl;
  26. }
  27. return 0;
  28. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Caught sqrt received negative argument