Try/Catch/Throw in C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aemado
    New Member
    • Sep 2007
    • 17

    Try/Catch/Throw in C++

    I am really new to the try/catch/throw concept, and can't figure out what is wrong with my code. Any suggestions?

    Code:
    #include <iostream>
    
    using namespace std;
    
    string msgZero = "Zero denominator\n";
    
    void inverse(long value, double& answer) {
    
      if (value > 0) {
    
        answer = 1.0/value;
    
      }
    
      else {
    
        if (value == 0) {
    
            throw msgZero;
    
        }
    
        else {
    
          throw value;
    
        } 
    
      }  
    
      return; }
    
      
    
    void fraction (long n, long d, double& result) {
    
       inverse(d, result);
    
       result = n * result; 
    
       return;
    
    }     
    
    int main () {
    
      while (true) {
    
        long numer, denom;
    
        double ans;
    
        cout << "Enter numerator and 
    
                denominator: ";
    
        if ((cin >> numer >> denom) == 0) break;
    
        try {
    
          fraction(numer, denom, ans);
    
        }
    
        catch (char* str) {
    
          cout << str; }
    
        cout << "Skipped the first catch block"<< endl;
    
        catch (long val) {    
    
          cout << "Negative denominator "<<
    
                   val<< endl; }
    
        cout << “*********”<< endl;
    
      }     
    
      return (0);
    
    }
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by aemado
    I am really new to the try/catch/throw concept, and can't figure out what is wrong with my code. Any suggestions?

    Code:
    #include <iostream>
    
    using namespace std;
    
    string msgZero = "Zero denominator\n";
    
    void inverse(long value, double& answer) {
    
      if (value > 0) {
    
        answer = 1.0/value;
    
      }
    
      else {
    
        if (value == 0) {
    
            throw msgZero;
    
        }
    
        else {
    
          throw value;
    
        } 
    
      }  
    
      return; }
    
      
    
    void fraction (long n, long d, double& result) {
    
       inverse(d, result);
    
       result = n * result; 
    
       return;
    
    }     
    
    int main () {
    
      while (true) {
    
        long numer, denom;
    
        double ans;
    
        cout << "Enter numerator and 
    
                denominator: ";
    
        if ((cin >> numer >> denom) == 0) break;
    
        try {
    
          fraction(numer, denom, ans);
    
        }
    
        catch (char* str) {
    
          cout << str; }
    
        cout << "Skipped the first catch block"<< endl;
    
        catch (long val) {    
    
          cout << "Negative denominator "<<
    
                   val<< endl; }
    
        cout << “*********”<< endl;
    
      }     
    
      return (0);
    
    }

    Issue 1)You are throwing a string and in the catch block catching char *.
    String is equivalent to char*
    To find this add catch(...)after the last catch block and you can catch the exception there

    Raghuram

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by gpraghuram
      Issue 1)String is equivalent to char*
      Is this a typo? I think you mean

      String is not equivalent to char *

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by Banfa
        Is this a typo? I think you mean

        String is not equivalent to char *

        Yes....Thats a Typo.

        Thanks for pointing it.



        Raghuram

        Comment

        Working...