welcome to
evildojo


C++ blog

Exceptions

#include <iostream>

using namespace std; 

double division(int a, int b);

int main() {
    int x = 50;
    int y = 0;
    double z = 0;

    try {
        z = division(x, y);
    } catch (const char * msg) {
        cerr << msg << endl;
    }

    return 0;
}

double division(int a, int b) {
    if (b == 0) {
        throw "Division by zero condition!";
    }
    return a/b;
}
#include <iostream>
#include <exception>
using namespace std;

struct MyException : public exception {
   const char * what () const throw () {
      return "C++ Exception";
   }
};
 
int main() {
   try {
      throw MyException();
   } catch(MyException& e) {
      std::cout << "MyException caught" << std::endl;
      std::cout << e.what() << std::endl;
   } catch(std::exception& e) {
      //Other errors
   }
}