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

  • Home
  • SEARCH
  • 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 9207631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:25:49+00:00 2026-06-18T00:25:49+00:00

I did bit of researching while trying to find proper way to implement exceptions

  • 0

I did bit of researching while trying to find proper way to implement exceptions in my code I came accross

    Throw by value, catch by reference

to be the recommended way of deal exceptions in C++ . I have a confusion about when the exception thrown gets out of scope.

I have following exception hierarchy

    ConnectionEx is mother of all connection exceptions thrown by dataSender
    ConnectionLostEx is one of the subclasses of ConnectionEx

so here is a sample code. In words DataSender instance is member of DataDistrubutor who calls functions on dataSender for example send() and DataSender throws ConnectionEx’s subclasses as exceptions in case of problems.

// dataSender send() function code chunk

try
{
   // some problem occured
       // create exception on stack and throw
   ConnectionLostEx ex("Connection lost low signals", 102);
   throw ex;
}
catch(ConnectionLostEx& e)
{
    //release resources and propogate it up
    throw ;
}

//A data distributor class that calls dataSender functions
try
{
    // connect and send data
    dataSender.send();  
}
catch(ConnectionEx& ex)
{
       // free resources
       // Is the exception not out of scope here because it was created on stack of orginal block?
       //propogate it further up to shows cause etc..

}

In C# or java we have a pointer like reference and that it valid all the way up, I am confused about the scope of exceptions given the exception is thrown by value when exactly does it get out of scope ??? and when caught as parent type ConnectionEx in this case can this be casted to get the real one back somwhere up in the chain of catch blocks ??

  • 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-18T00:25:50+00:00Added an answer on June 18, 2026 at 12:25 am

    A copy of the exception is thrown, not the original object. There’s no need to instantiate a local variable and throw it; the idiomatic way to throw an exception is to instantiate it and throw it on the same line.

    throw ConnectionLostEx("Connection lost low signals", 102);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I did a bit of research around here and was unable to find
I am trying to implement a bit torrent client. The first step is decode
I did a bit of searching but couldn't find a definitive answer on this,
I did a bit of searching but came up empty on this. I'm looking
I did a bit of Googling, and I found that the following code does
Sanity-check questions: I did a bit of googling and discovered the correct way to
So I did a bit of C programming a while ago and basically forgot
I did a little bit of Web Programming here and there but I never
I've searched quite a bit and found a few solutions that did not end
I have windows 7 (64-bit) and just upgraded IE9 in my machine. I did

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.