code_junkie

Portably handle exceptional errors in C++

Nov 10th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. struct my_exception_type : public logic_error {
  2. my_exception_type(char const* msg) : logic_error(msg) { }
  3. };
  4.  
  5. try {
  6. throw my_exception_type("An error occurred");
  7. } catch (my_exception_type& ex) {
  8. cerr << ex.what << endl;
  9. }
  10.  
  11. try {
  12. // …
  13. }
  14. catch (...) {
  15. }
  16.  
  17. int foo(int x) try {
  18. // body of foo
  19. } catch (...) {
  20. // be careful what's done here!
  21. }
  22.  
  23. try
  24. {
  25. /* code that may throw exceptions */
  26. }
  27. catch (Error1 e1)
  28. {
  29. /* code if Error1 is thrown */
  30. }
  31. catch (Error2 e2)
  32. {
  33. /* code if Error2 is thrown */
  34. }
  35. catch (...)
  36. {
  37. /* any exception that was not expected will be caught here */
  38. }
Add Comment
Please, Sign In to add comment