Solved

Defining appropriate Exception Handling classes

Posted on 2003-12-01
4
260 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 300 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now