Answers for "declare a function exception c++"

C++
3

declare and define exception c++

// using standard exceptions
#include <iostream>
#include <exception>
using namespace std;

class myexception: public exception {
  virtual const char* what() const throw() {
    return "My exception happened";
  }
} myex; // declare instance of "myexception" named "myex"

int main () {
  try {
    throw myex; // alternatively use: throw myexception();
  } catch (exception& e) { // to be more specific use: (myexception& e)
    cout << e.what() << 'n';
  }
  return 0;
}
Posted by: Guest on September-16-2020

Code answers related to "declare a function exception c++"

Browse Popular Code Answers by Language