LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-18-2012, 01:34 AM   #1
Hamed
LQ Newbie
 
Registered: Sep 2010
Posts: 25

Rep: Reputation: 4
Question [C++] Static class and exception handling


In the name of Allah the most benefition the most merciful

Peace upon to you

class testA
{
testA(int a,int b) throw (int)
{
throw 227;
}
}

class testB
{
public::
static void init()
{
}
private:
static testA object;
}
testA testB:bject(92,100);

int main()
{
try {
testB::init();
}
catch (int &e)
printf("exception ocurred");
}

return 0;
}

This isn't my real code but I want to show you I have something like this and I cann't handle the exception.
Please,tell me how to solve this problem

Thanks in advance
 
Old 02-18-2012, 03:43 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
1) please use [code] ... [/code] tags when posting code.
2) Your code contains a lot of error. It doesn't even compile without modifications.

i) class definitions need to end with a semicolon:

Code:
class testA {
    ...
};

class testB {
    ...
};
ii) if you want to call a constructor of static member testA that is declared within testB, you should make the constructor public:

Code:
class testA
{
public:
    testA(int a,int b) throw (int) { ... }
}

...

testA testB:bject(92,100);

iii) one colon after public is enough:
Code:
class testB
{
public::
    ...
}
iv) your main() is missing a brace:

Code:
int main()
{
    try {
        testB::init();
    }
    catch (int &e) 
    {
        printf("exception ocurred");
    }

    return 0;
}
Anyway, if you fix all the errors, the proram will crash with an unhandled exception error. Why? Just read your code:
Code:
testA testB:bject(92,100);
this is where you initialise your static member. This is actually done before main() is called and therefore not within your try -- catch construct. Why would testB::init, which is in your try block cause an exception being thrown is beyond me.

Last edited by millgates; 02-18-2012 at 04:44 AM. Reason: typo
 
Old 02-18-2012, 04:06 AM   #3
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Is there a reason you can't catch the exception in the constructor?
 
Old 02-19-2012, 10:59 AM   #4
Hamed
LQ Newbie
 
Registered: Sep 2010
Posts: 25

Original Poster
Rep: Reputation: 4
Smile [C++] Static class and exception handling

In the name of Allah the most benefition the most merciful

Peace upon to you
Thanks a lot for reading and correcting my codes and replying.

My crash problem was solved by using static pointer.

Code:
class testA
{
  public:
    testA(int a,int b) throw (int)
  {
    throw 202;
  }
  ~testA() throw (int)
  {
    throw 92;
  }
};

class testB
{
  private:
    static testA *obj;
  public:
    static void init()
    {
      obj = new testA(110,227);
    }
    static void kill()
    {
      delete obj;
    }
};
testA *testB::obj;

int main()
{
  try {
    testB::init();
  }
  catch (int &e) {
    cout << e << ": exception handled" << endl;

  }

  return 0;}
}
 
Old 02-19-2012, 12:59 PM   #5
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 209

Rep: Reputation: 66
One thing: throwing from a destructor can take down your entire program. Basically, if exception handling is unwinding the stack and during the unwind a destructor throws, C++ calls terminate() because there's no recursive-exception mechanism -- howitzer-proof error recovery like that is the province of mainframe OS's, which UNIX isn't and C++ can't mandate. Fortunately it's easy enough to avoid, you can build your own by instead leaving instructions for the catch clause if the destruction failure leaves necessary cleanup unfinished and mustn't crash your code`.
 
  


Reply

Tags
test


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
exception handling abdolah Linux - General 1 11-10-2010 05:58 AM
How to initialize a static array of a class in a static member function lali.p Programming 9 02-16-2008 09:27 AM
Exception Handling vipinsharma Programming 2 09-25-2005 11:05 AM
Exception Handling C++ Hady Programming 3 07-26-2005 11:07 PM
help createing exception class from base STL exception qwijibow Programming 4 04-20-2005 05:23 AM


All times are GMT -5. The time now is 10:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration