C++ exceptions in wxWidgets constructors Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
davebee
Earned some good credits
Earned some good credits
Posts: 106
Joined: Fri Oct 06, 2006 1:39 am

C++ exceptions in wxWidgets constructors

Post by davebee » Wed Oct 25, 2006 11:46 pm

How should I approach this issue?

I mean, many of the samples that come with wxWidgets, call the 'new' operator in their constructors, but I haven't seen any 'try' clause there...

Are these samples a recommendation of how to program in wxWidgets or merely skeletons that assume that non of the calls will fail?

How do you deal with this issue and what do you recommend doing about it?

Aside from enclosing everything in a constructor (that uses wxWidgets) in one big 'try/catch' clause, I currently see these options:

1. Set a flag that signals that the construction suceeded/failed.
2. Throw my own exception upon failure (and soon my code will be full of try/catch clauses.

Is there a "recommended wxWidgets way" to deal with this?

Do any of the wxWidgests classes throw exceptions?

Thanks,
Dave
wxMSW-2.6.3 / Visual C++ 2005 EE / Windows XP SP2

(was: wxMSW-2.6.3 / Visual C++ 6.0 SP6 / Windows 2000 SP5)

Alan-Shouls
Knows some wx things
Knows some wx things
Posts: 33
Joined: Mon Aug 07, 2006 3:26 pm
Contact:

Post by Alan-Shouls » Thu Oct 26, 2006 10:55 am

Hi Dave,

I am relatively new to wx - so I am sure that there are people who can answer better than me. What I understand is this:

wx predates exception handling and currently does not use or throw any exceptions. From what I have seen of the code it looks like it is generally written on the assumption that new does not fail. I have inferred this from seeing code of the form

Code: Select all

var = new thing;
 :
 :
delete var;
within the wx sources.

All other errors are handled internally without exceptions.

From this I would imagine that if you are writing code then you too should assume that new does not fail - there is no point in making allowances for new failing if the framework does not. (I could be wring about this)

If you want to use exceptions within your own code you can - there is an example on one of the samples that has exceptions.

If you want to find out if/when the issue of exception handling & support will change in wx (I hope it will) I would suggest that you join the wxdev and ask the question there ( you can join at http://www.wxwidgets.org/support/maillst2.htm). This is where all the key developers of wx communicate and I do not doubt that they will be able to give you their thoughts.

Best regards

Alan Shouls

Cursor
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Aug 29, 2004 3:09 pm
Location: Grenoble, France
Contact:

Post by Cursor » Thu Oct 26, 2006 11:13 am

Alan-Shouls is absolutly right.

Moreover, it is why some wxClasses have somme Ok() or IsOk() functions in order to know if the object if usable.
What is little and green, witch go up and down ??
Yoda playing with the force.

Post Reply