Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Defining appropriate Exception Handling classes

Posted on 2003-12-01
4
Medium Priority
?
268 Views
Last Modified: 2010-08-05
Hello,

I'd like to define exception classes that would be thrown/caught when necessary.

The example exception1 class below would be thrown when an integer entered is out of range.
Question1) Could the class be changed so that it can contain more meaningful information? e.g the value entered.

The exception "message" will then be passed back (using a return statement) to the calling function where the appropriate catch statement will reside.
i.e. the calling function will check the return status of the called function and check for the existence of "Error"  in the first 5 chars.
The info can then be used for diagnostics.

Question 2.) Also, Any possible problems with this approach?


-------------------------------------------
class  Exception1 {
public:
          Exception1()
            : message ("Error: Invalid value entered") {}
        const char *what() const { return message;}
    private:
        const char *message;
    };

-------------------------------------------

N.B. As this is really 2 questions, I've assigned 150 points to each one giving a total of 300 points...

Thanks for your help,


WesLondon
0
Comment
Question by:weslondon
4 Comments
 
LVL 9

Expert Comment

by:tinchos
ID: 9851475
Wes

It all depends in what info you want to store of the exception.

The Exception class can have any info you want and any methods you like. There are no restrictions about it.

Regarding number 2, I would suggest that you store a string instead a char * and maybe add a constructor that takes the message to be used.

class  Exception1 {
public:
         Exception1()
           : message ("Error: Invalid value entered") {}
         Exception1( const string & msg ) : message( msg ) {}
           : message ("Error: Invalid value entered") {}
       const char *what() const { return message.c_str();}
   private:
       string message;
   };

Hope this helps

Tincho
0
 
LVL 86

Accepted Solution

by:
jkr earned 900 total points
ID: 9851497
>>Question1) Could the class be changed so that it can contain more meaningful information?

Sure, e.g.

class  Exception1 {
public:
         Exception1()
           : message ("Error: Invalid value entered"), nInvalidValue(0) {}

         Exception1(int nVal)
           : message ("Error: Invalid value entered"), nInvalidValue(nVal) {}
       const char *what() const { return message;}
   private:
       const char *message;
       const int nInvalidValue;
   };

>>Question 2.) Also, Any possible problems with this approach?

No. But, why don't you use the 'regular' C++ mechanism of throwing and catching exceptions, e.g.

int enter_value () {

   // ... code ...

   // error condition
   if ( value > limit) {

      throw new Exception1 ( value);
  }
}

void foo () {

int n;

   try {

    n = enter_value();

  } catch ( Exception1* p) {

    // do diagnostics

    delete p;
  }
}
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 9852223
Exception1() : message ("Error: Invalid value entered") {}

I don't think that this is a good idea.  Really you should be doing this:

Exception1::Exception1()
{
   char[] default = "Error: Invalid value entered";

   message = new char[strlen("Error: Invalid value entered") + 1];
   strcpy(message, default);
}

Then you can be sure that the memory is allocated correctly.  Then you will also have to lose the 'const' before the declaration of message.

You will also need to rewrite the destructor hence to include the line:

delete[] message;

HTH
0
 
LVL 1

Expert Comment

by:travd
ID: 9854605
Sounds like homework to me!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question