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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:38:08+00:00 2026-06-08T05:38:08+00:00

class A{ public: A() { throw string(exception A); }; }; class B{ A a;

  • 0
class A{
    public:
        A() { throw string("exception A"); };
};

class B{
    A a;
    public:
        B() try : a() {} catch(string& s) { cout << &s << " " << s << endl; };
};

int main(){    
    try{
        B b;
    }catch(string& s){
        cout << &s << " " << s << endl;
    }
    return 0;
}

The output is:

0x32c88 exception A
0x32c88 exception A

Since the exception was already caught in the constructor of B, why it still occur in the main function?

  • 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-08T05:38:11+00:00Added an answer on June 8, 2026 at 5:38 am

    When the flow of contol reaches the end of the handler for a function-try-block of a constructor, the caught exception will automatically be re-thrown.

    You cannot suppress an exception thrown during the construction of a base class or member in a derived class constructor as this would lead to a constructed derived object with a base or member that had failed to be constructed.

    This GOTW is relevant: http://www.gotw.ca/gotw/066.htm

    From ISO/IEC 14882:2011 15.3 [except.handle] / 15:

    The currently handled exception is rethrown if control reaches the end of a handler of the function-try-block of a constructor or destructor. […]

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

Sidebar

Related Questions

class Test { public static void main(String[] args) throws Exception { Test t =
import javax.swing.*; public class test { public static void main(String[] args) throws Exception {
If I run the following program: class Runit{ public static void main(String[] argsWut) throws
import java.io.*; import java.util.*; public class Readfilm { public static void main(String[] args) throws
Consider the following code: class A { public: virtual void f() throw ( int
Why it's impossible to throw exception from __toString()? class a { public function __toString()
The below code in Java throws Null pointer exception. public class New{ int i;
I have made a class to try exceptions: class precExcp:exception { public: virtual const
I'm trying to make this code generic: public final Object unwrap(Class arg0) { throw
`Adapter class ::: public class YAdapter extends ArrayAdapter<String> { ArrayList<String> items; Context context; private

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.