Solved

Defining appropriate Exception Handling classes

Posted on 2003-12-01
4
265 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

751 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