Answers for "what is 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
0

cpp get exception type

#include <iostream>
#include <cxxabi.h>

const char* currentExceptionTypeName()
{
    int status;
    return abi::__cxa_demangle(abi::__cxa_current_exception_type()->name(), 0, 0, &status);
}

int main()
{
    try {
        throw std::string();
    } catch (...) {
        std::cout<<"Type of caught exception is "<<currentExceptionTypeName()<<std::endl;
    }

    return 0;
}
Posted by: Guest on July-15-2021

Browse Popular Code Answers by Language