fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. void MightGoWrong()
  7. {
  8. bool error = true;
  9. if (error)
  10. {
  11. throw string("Something else went wrong");
  12. }
  13. }
  14.  
  15. /**
  16.  * Output on console: Int exception occurred: 14947880
  17.  */
  18. int main() {
  19. try
  20. {
  21. MightGoWrong();
  22. }
  23. // If string or char pointer catch block goes to top, that block will execute
  24. catch (int e)
  25. {
  26. cout << "Int exception occurred: " << e << endl;
  27. }
  28. catch (string &e)
  29. {
  30. cout << "String exception occurred: " << e << endl;
  31. }
  32. catch (char const * e)
  33. {
  34. cout << "Char exception occurred: " << e << endl;
  35. }
  36. return 0;
  37. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
String exception occurred: Something else went wrong