Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:49:59+00:00 2026-06-03T22:49:59+00:00

Note: I can’t use anything that is default. I am trying to make a

  • 0

Note: I can’t use anything that is default.

I am trying to make a very simple exception handling routine or at the very least make something that looks the part. I don’t want to do very much, just throw an exception and print an error message.

in .h

class MyException {
    protected: string message;

    public:

        MyException (string mes) {
            this->message = mes;
        }

        MyException (); // is this necessary ? does it do anything ?

        string getMessage() const {
            return this->message;
        }
};

What I’d want is to have a “PersonException” and “ActivityException”. Might use a template but not sure if that would work out.

class PersonException:public MyException {

    public:

        PersonException (string message):MyException(message) {

        }
};


class PersonValidator {

    public:

        PersonValidator (Person p) throw (PersonException);
};

in .cpp

void PersonValidator::PersonValidator(Person p) throw (PersonException) {
    if (p.getPhone < 0) {
        throw PersonException ("Person Number is invalid");
}

What here is wrong or cumbersome, how could it be done better ? and where do I actually print the error message ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:50:01+00:00Added an answer on June 3, 2026 at 10:50 pm

    1) The default constructor is not necessary, at least the way you have the code now, so you can remove

     MyException ();
    

    2) It’s recommended to derive exceptions from std::exception.

    3) You can catch your exceptions by catching a MyException&, and print the message there :

    try
    {
        PersonValidator validator(Person());
    }
    catch(const MyException& ex)
    {
        std::cout << ex.getMessage();
    }
    

    4) Avoid using directives in headers. Your syntax suggests you have a using namespace std; in the header. That’s wrong, you should favor full name qualification, at least in headers:

    protected: std::string message;
    MyException (std::string mes)
    

    etc.

    5) Favor pass by const reference instead of pass by value, for complex types:

    MyException (const std::string& mes)
    
    PersonValidator (const Person& p)
    

    6) Aim for const correctness:

    std::string getMessage()
    

    should be:

    std::string getMessage() const
    

    since it doesn’t change any members.

    7) Use initialization lists:

     MyException (string mes) {
         this->message = mes;
     }
    

    becomes

     MyException (string mes) : message(mes) {
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I go to create my status bar overlay, I note that I can
Note: when I say static string here I mean memory that can not be
How can I remove the unwanted whitespace in following example? Note 1. that there
Can I write XSD schema for this? Please note that the category is repeated
namespace WcfService1 { // NOTE: You can use the Rename command on the Refactor
Is there way to list all printers on a domain? note:I can use delphi
I have an entity Tag and an entity Note . A note object can
Note This is not a REBOL-specific question. You can answer it in any language.
How can I update some Windows Service Seperated Assemblies without restarting the service? Note:
How can I retrieve the first and last record from a table. Note :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.