?
Solved

Exception Handling in C++

Posted on 2003-03-15
4
Medium Priority
?
182 Views
Last Modified: 2010-04-01
I have to write an exception class that will output a different message for each different situation, how do I even start writing an exception class. Please help. Thanks

mb
0
Comment
Question by:mb650cd
[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
  • 2
4 Comments
 
LVL 1

Expert Comment

by:PerlChaser
ID: 8145655
You can create an exception class by simply using a message in it.  For example, if you were checking for invalid digits, you would use

InvalidDigit::InvalidDigit()
{ msg = "That is not a valid digit.\n"; }

Then, in your code you could have

if(!(isdigit(n)))
  throw InvalidDigit();

Hope this helps!
~PerlChaser~
0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 320 total points
ID: 8145681
for ex:

class Exception {
   string  msg;
  public:
   Exception(string mesg) : msg(mesg) {}
   const string what() { return msg; }
};

then throw it as PerlChaser said when you write your code (1). You then use a try-catch (or try-catch-catch-...-finally) block when writing codes that use the code (1).

code:
void Check(int n) {
 // borrow code from PerlChaser
}

then:
try {
 Check('a');
}
catch (Exception ex) {
   cout << ex.what() << "\n";
}

something like that. =)
0
 
LVL 1

Expert Comment

by:PerlChaser
ID: 8606495
mb650cd,

Please wrap up this question; you have had sufficient time to evaluate these comments.
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9495774
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: n_fortynine

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 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 technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

764 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