Week 6

Notes 6

Exception Handling

  • try{
      // do something with
      // potential thrown
      // exception.
    } catch( exception ){
      // handle the error
    }
    
    double divide(double a, double b) {
      if(b==0)
        throw("Cannot divide by zero\n");
      return(a/b);
    }
    
    ...
    
    try{
      cout << divide(4,0);
    } catch(const char* err) {
      cout << err << endl;
    }
    
  • Exception class
  • Multiple Exceptions
  • Exception class (extended)
  • Default Exception