fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. void exampleFunction(int value) {
  7. if (value == 0) {
  8. throw runtime_error("Runtime error: value cannot be zero");
  9. }
  10. else if (value < 0) {
  11. throw invalid_argument("Invalid argument: value cannot be negative");
  12. }
  13. else {
  14. cout << "Value is valid" << endl;
  15. }
  16. }
  17.  
  18. int main() {
  19. try {
  20. exampleFunction(0); // This will throw a runtime_error
  21. }
  22. catch (const invalid_argument& e) {
  23. cout << "Caught exception: " << e.what() << endl;
  24. }
  25. catch (const runtime_error& e) {
  26. cout << "Caught exception: " << e.what() << endl;
  27. }
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0s 5272KB
stdin
Standard input is empty
stdout
Caught exception: Runtime error: value cannot be zero