473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

try...catch in constructor

Is it good style to use a try...catch clause in the constructor or is it
something that is frowned upon?
May 22 '06 #1
3 10810
If you can recover from an error in the constructor, it's fine. Otherwise,
no.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.

"Martijn Mulder" <i@m> wrote in message
news:44***********************@news.wanadoo.nl...
Is it good style to use a try...catch clause in the constructor or is it
something that is frowned upon?

May 22 '06 #2
The same rules about try...catch that apply to other methods apply to
constructors as well: catch only what you can deal with intelligently.
Other than that, there's no difference that I know of between event
handling / raising in constructors versus other methods.

May 22 '06 #3
On Mon, 22 May 2006 20:19:36 +0200, "Martijn Mulder" <i@m> wrote:
Is it good style to use a try...catch clause in the constructor or is it
something that is frowned upon?


Try-Catch in constructors work exactly as try-catch in methods. Only
catch exceptions if you actually handle it, otherwise let the
constructor throw an exception.

The only exception that I can think of, happens when dealing with
IDisposable objects. Consider the following class

class MyDisposableClass : IDisposable
{
AnotherIDisposableClass memberDisposable;

public MyDisposableClass(string s)
{
memberDisposable = AnotherIDisposableClass();

if(s == null)
throw new ArgumentException(); //Creates a resource leak
}

public void Dispose() //Vert
{
if(memberDisposable != null)
memberDisposable.Dispose();
}
}

In the above code the memberDispoable variable will be created, but it
won't be disposed of. This is an easy to miss resource leak, since it
falls outside of the usual IDisposable/using/try-finally pattern.

This can be fixed in three ways that I can think of.

* Making sure the constructor doesn't cause any exceptions by catching
them all. Catching all exceptions is however bad style, so this is the
worst solution.

* Moving the creation of the IDisposable objects out of the
constructor (or to the end of the constructor if there only is one).

* Using try-catch and call dispose manually in the constructor, like
the following example:

public MyDisposableClass(string s)
{
try
{
memberDisposable = AnotherIDisposableClass();

if(s == null)
throw new ArgumentException(); // This can create a problem
}
catch
{
this.Dispose();
throw;
}
}

--
Marcus Andrén
May 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
3
by: valued customer | last post by:
Is there a more concise way to do something like the the desired code below? The gripe is with the try-catch syntax. It takes *way* too many lines of code to evaluate a conditional expression...
12
by: Andrew Schepler | last post by:
When compiled with Visual C++ .NET 2003 (only), the program below aborts as though no matching catch clause is present. If the copy constructor of A is made public, it successfully catches the...
5
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
1
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw...
10
by: Rahul | last post by:
Hi Everyone, I have the following exception class, class E1 { }; class E2 {
10
by: George2 | last post by:
Hello everyone, Here is a sample from Dr. Dobb C++. In the analysis, the code is bad below. But I do not think the code is bad, 1. if bad_alloc is thrown in new int, we just catch it and...
4
by: Sunil Varma | last post by:
Hi, Here is a piece of code where the constructor throws an exception. class A { int n; public: A() try{
11
by: Peter Jansson | last post by:
Dear newsgroup, In the following code, it looks as though the exception thrown in the constructor is not caught properly. I get this output: ---- standard output ---- Base() constructor....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.