Solved

Defining appropriate Exception Handling classes

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 89
Unresolved External Symbols 3 105
Autosar OS Multicore Share Resources confusion ? 2 71
Finding Divisors 5 16
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

860 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