define exception in c++ code example
Example 1: java define custom exception
public class CustomException extends Exception {
public CustomException(String errorMessage) {
super(errorMessage);
}
}
Example 2: declare and define exception c++
#include <iostream>
#include <exception>
using namespace std;
class myexception: public exception {
virtual const char* what() const throw() {
return "My exception happened";
}
} myex;
int main () {
try {
throw myex;
} catch (exception& e) {
cout << e.what() << '\n';
}
return 0;
}
Example 3: c++ try
try {
} catch (...){
}