user49157 user49157 - 3 months ago 9
C++ Question

pass class variable to exception in C++

This is a (modified) problem from a test last week.
I was given an exception class with a predefined number in it:

class ErrorException {

/**
* Stub class.
*/
private :static long ErrorCode;

public: ErrorException( string str) {
cout <<str;
}
};

long ErrorException::ErrorCode = -444;


I think I was supposed to do was catch the exception and then return the number as an error code, but I could not figure out how to get the number in. I could make the catch return a string but not the number as string:

#include "stdafx.h"
#include <iostream>
#include "ErrorException.h"

#include "errno.h""
#include <string>;
class FillerFunction {


public :

virtual int getFillerFunction(int x) throw (ErrorException) = 0;

} // this notation means getFillerFunction is always throwing ErrorException?

double calculateNumber(int y){
//.....
try{
if (y !=0){
throw(ErrorException(?????))
}
};

double catchError(){
catch(ErrorException& x);
};


I eventually made it return the string "error" which is no better than using an if statement. I've looked up other catch-throw examples in c++ and dynamic exceptions, but I can't find an example with an exception grabbing a variable defined in the class.How do I access the ErrorCode, save changing the return type of ErrorException()?

Answer

In your throw you're constructing an exception object. If you prefer to pass a number, you must provide the appropriate constructor.

Quick and dirty:

class ErrorException  {

private :
    static long ErrorCode;

public: 
    ErrorException( string str) {
                cerr <<str<<endl;
        }
    ErrorException( long mynumeric code) {
                cerr <<"error "<<mynumericcode<<endl;
        }
};

The catching should look like:

double calculateNumber(int y){
    try {
         if (y !=0){
            throw(ErrorException(227));   
         }
    } catch(ErrorException& x) {
         cout << "Error catched"<<endl; 
    }
 }

Must be elaborated further

It's unusual to to print something in the exception constructor. You'd better populate the information needed in the catch, so that these information could later be accessed with the appropriate getter. The printing would then occur in the exception handler.

If you have a static error code, I suppose that somewhere you have a function that returns the last error code whatever happened. So maybe you'd update this code (but how do you intend to update it with the existing string alternative ?

Here how it could look like:

class ErrorException  {    
private :
    static long LastErrorCode;
    long ErrorCode; 
    string ErrorMessage;     

public: 
    ErrorException(long ec, string str) : ErrorCode(ec), ErrorMessage(str) 
        {
                LastErrorCode = ec; 
        }
    static long getLastError() const { return LastErrorCode; }  // would never be reset
    long getError() const { return ErrorCode;  }
    string getMessage() const { return ErrorMessage; }
};

The catching should look like:

double calculateNumber(int y){
    try {
         if (y !=0){
            throw(ErrorException(227,"y is not 0"));   
         }
    } catch(ErrorException& x) {
         cerr << "Error "<<x.getError()<<" : "<<x.getMesssage()<<endl; 
    }
    cout << "Last error ever generated, if any: " << ErrorException::getLastErrror()<<endl;
 }

Neverthesess, I'd advise you to have a look at std::exception before reinventing the wheel.

Comments