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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:48:13+00:00 2026-06-09T14:48:13+00:00

I saw many examples but I am not able to understand, how to use

  • 0

I saw many examples but I am not able to understand, how to use try catch with a simple constructor, I wrote a sample program:

class A
 {
   public:
    try {
       A()
        { cout << "in costr\n"; throw 10;}
    }//try closed
   catch (int a)
{ cout << "caught 1 \n"; }

 };

main()
 {
   A *ptr = new A;
   }
  1. This program gives a compilation error
  2. If exception is caught, what happens to object ??
  • 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-09T14:48:15+00:00Added an answer on June 9, 2026 at 2:48 pm

    The try/catch code is supposed to be together, you can’t have one without the other. Something like this is what you’re after:

    A *ptr;
    try {
        ptr = new A();
    } catch (int a) {
        cout << "caught 1\n";
    }
    

    See the following program for a complete working example:

    #include <iostream>
    
    class A {
        private:
            int a;
        public:
            A() { a = 7; throw 42; }
            int getA() { return a; }
    };
    
    int main (void) {
        A *ptr;
        try {
            ptr = new A();
        } catch (int b) {
            std::cout << "Exception: " << b << '\n';
            return -1;
        }
        std::cout << "Value: " << ptr->getA() << '\n';
        return 0;
    }
    

    With the throw 42 in there, you see:

    Exception: 42
    

    meaning that main has caught the exception coming from the constructor. Without the throw, you see:

    Value: 7
    

    because everything has worked.


    The main problems with your code seem to be:

    • You have a try statement where it shouldn’t be. Try/catch blocks should generally be within a function or method, you have it immediately after the public keyword.

    • If you’re throwing an exception from the constructor, you don’t catch it in the constructor. Instead you catch it in the code that called the constructor (main in this case).

    • As previously mentioned, try and catch go together, they’re not standalone entities.


    If you are trying to throw and catch within the constructor, you’ll still need to put it within the constructor itself, something like:

    #include <iostream>
    
    class A {
        private:
            int a;
        public:
            A() {
                try {
                    a = 7;
                    throw 42;
                } catch (int b) {
                    std::cout << "Exception A: " << b << '\n';
                    throw;
                }
            }
            int getA() {return a;}
    };
    
    int main(void) {
        A *ptr;
        try {
            ptr = new A();
        } catch (int b) {
            std::cout << "Exception B: " << b << '\n';
            return -1;
        }
        std::cout << "Value: " << ptr->getA() << '\n';
        return 0;
    }
    

    which gives you:

    Exception A: 42
    Exception B: 42
    

    Note specifically how the try/catch block is both complete and within the constructor function.

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

Sidebar

Related Questions

I saw many examples and tried to follow but none of them would work.
I saw many threads with this tittle, but no one really speak about reuse
Earlier I asked a question about why I see so many examples use the
I look up a lot through this problem while i saw many post abt
Fastest way to uniqify a list in Python without preserving order? I saw many
I saw % in many codes. Can you explain to me its purpose or
I saw in many application like Zara, Zara Home, D&G.. that instead of using
I've read about many to many relationship in rails 3 and saw that HABTM
I have long cycles of GC. from checks I saw there are too many
I saw the docs on the Instagram developers page. http://instagram.com/developer/endpoints/ But I want to

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.