Catching class type exceptions : Exception type « Exceptions « C++ Tutorial






#include <iostream>
#include <cstring>
using namespace std;

class MyException {
public:
  char message[80];
  int what;

  MyException() { *message = 0; what = 0; }

  MyException(char *s, int e) {
    strcpy(message, s);
    what = e;
  }
};

int main()
{
  int i;

  try {
    cout << "Enter a positive number: ";
    throw MyException("Not Positive", -1);
  }
  catch (MyException e) { // catch an error
    cout << e.message << ": ";
    cout << e.what << "\n";
  }

  return 0;
}
Enter a positive number: Not Positive: -1








6.4.Exception type
6.4.1.Different types of exceptions can be caught
6.4.2.how to restrict the types of exceptions that can be thrown from a function.
6.4.3.Catch double exception
6.4.4.Catching class type exceptions
6.4.5.Catching Class Types