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 5956827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:18:48+00:00 2026-05-22T18:18:48+00:00

I want to build a class with functions that may throw exceptions that I

  • 0

I want to build a class with functions that may throw exceptions that I want to catch when I use it.
I inherit my_exception from the standard exception class.
I implement the what() function so that it returns a string that is stored in a private string variable

I think it would be better to define the exception as a nested class, the way it’s done in iostream library with ios_base::failure.

What I’m less sure about, is where and how I should define the object of my_excpetion. I wish I could see the inner code of the iostream functions and see how they did it.
I have thought about several options:

  1. For each reason of exception I can define a static instance of my_exception, with a constructor that gets a string and save it to my private string pointer.

  2. For each reason of exception I can define another class that inherit from my_exception and implement what as a function that returns a constant string (the reason).
    I can hold an instance of each of that exceptions sub-classes, or to throw the type.
    BTW, when do we usually throw type and not instance?

  3. I guess it’s wrong: each time I want to throw an exception, to create a new my_exception with a constructor that gets a string. This is done in Java, but as I understand it will be problematic in C++ because the exception should be deleted somewhere. Right?

I think that the 1st one is the right one, is it?
Are there more standard options?

Thank you very much!

  • 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-05-22T18:18:49+00:00Added an answer on May 22, 2026 at 6:18 pm

    Short answer: You are going to want to throw the exceptions as objects, rather than as pointers. You will catch them as references.

    Longer answer: all of the options you list are valid. In general, the reason you are going to want to throw an object, rather than a pointer, is becuase of the choices you give yourself and your clients when the exception is caught.

    If you catch by pointer, catch (my_exception* e) then you don’t know from looking at it whether you should delete the memory or not.

    If you catch by value, catch (my_exception e) then you have a risk of slicing, if the exception object ends up being a base class with some other derived classes.

    Catching by reference has neither of these problems. If you write catch (my_exception& r) then you can catch polymorphic objects, and you don’t have to worry about releasing the memory.

    So, to answer your other question, when you throw, just throw a temporary object: throw my_exception(). This creates a temporary object that is (probably) copied when thrown, caught by reference, and destroyed automatically when it goes out of scope at the end of your catch block. (This is actually another benefit of catch-by-reference over catch-by-value, since catch-by-value creates yet another copy when it’s caught.)

    As for your other, derived exception classes, it’s a style choice. Deriving from my_exception with a different what() implementation is pretty standard. I wouldn’t say you need to get fancy with storing the strings or instances in static objects – they’re small, and constructing one is going to take practically no time compared to the process of unwinding the stack when the exception is thrown.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build a DLL Class Library use COM Interop, with C#, target
I want to use the Html Helper class to build, for example, an Html.ActionLink
I want build a sketch pad app on iPhone, I assume that this type
I want to build a lightweight linux configuration to use for development. The first
I want to build a bot that asks someone a few simple questions and
I want to let the compiler build the connections of functions for a physics
This page shows how to call C++ functions from within QML. What I want
i need to build a c++ project that exports functions to c project this
I have a class that is getting exported from C++ to Python in SWIG.
I'm trying to build a list of functions that can be used for arbitrary

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.