-
September 17th, 2009, 02:03 AM
#1
Exception in constructor
Is it possible to throw exception from a constructor?
class A
{
A()
{
//some code
throw;
}
}
-
September 17th, 2009, 02:09 AM
#2
Re: Exception in constructor
Please use code tags like this : [SIGPIC][/SIGPIC]
Code Your Dreams
-
September 17th, 2009, 02:10 AM
#3
Re: Exception in constructor
It´s possible and perfectly OK for a constructor to throw an exception. In a constructor you don´t have any other options to indicate failure because it does not return anything (OK, OK, you can set a global error flag, but no one really wants that).
- Guido
-
September 17th, 2009, 02:18 AM
#4
Re: Exception in constructor
Please use code tags like this : [SIGPIC][/SIGPIC]
Code Your Dreams
-
September 17th, 2009, 06:13 AM
#5
Re: Exception in constructor
Just remember to think carefully about memory management when you start throwing exceptions.
Rich
Visual Studio 2010 Professional | Windows 7 (x64)
Ubuntu
-
September 17th, 2009, 10:20 AM
#6
Re: Exception in constructor
holestary's link addresses your question by recommending that exceptions be thrown from a constructor to indicate failure. This article elaborates on Rich2189's advice: GotW #66: Constructor Failures.
-
September 18th, 2009, 05:57 AM
#7
Re: Exception in constructor
Originally Posted by laserlight
Awesome article! *Devours*.
Originally Posted by www.gotw.ca
Moral #7: Prefer using "resource acquisition is initialization" to manage resources. Really, really, really. It will save you more headaches than you can probably imagine.
This being the most important point I think; well following that advice will lead to the least amount of headaches.
Rich
Visual Studio 2010 Professional | Windows 7 (x64)
Ubuntu
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|