Hi!
I want to know, under what circumstances you use try/catch blocks in constructors and destructors Example
Thanks
Hi!
I want to know, under what circumstances you use try/catch blocks in constructors and destructors Example
Thanks
You use them under the same circumstances you'd use them anywhere else: when you know what kind of exception might happen, and you know how you want the code to behave if it does.
For instance, you might have a class whose implementation hides the complexities of interoperating with some resource that might not always be available. Part of the complexity you might hide is the exception that gets thrown when the resource isn't there - instead of an exception, your constructor would set a public ResourceAvailable flag to false, and it would be up to the instantiating method to check that flag.
Or you might have a constructor with numeric parameters that might throw a divide-by-zero exception for some values. Should that actually throw an exception in the code that creates it, or should the object go into some predefined state if it's been created with bad parameters It all depends on how the class will be used.
Try/catch blocks can be used in any program for handling exceptions,whether it involve constructor/destructor or not.Let me explain this with the help of an example using constructor:
consider a class Distance.A Distance object has an integer value of feet,which should always be less than 12.0.A problem is this it could not protect itself if user initialized an object with an inches value of 12.0 or greater.This could lead to trouble when the class tried to perform arithmetic.Therefore here v can use an exception to handle this kind of error.....
class Distance
{
int feet;
float inches;
public:
class InchesEx{ }; //exception class
Distance(int ft,float in)
{
if (in>=12.0) //if inches too big
throw InchesEx( ); //throw exception
feet=ft;
inches=in;
cout<<feet<<inches;
} };
int main( )
{
try { distance dist1(17,3.5); }
catch(Distance::InchesEx)
{ cout<<"Initialization error"; }
return 0;
}
Hi!
Thanks for the reply! But when do you use try/catch blocks indestructors Example
Hi!
Actually, I was asked to give an example of using try/catch in a destructor at an Accenture interview!
Thanks
Are you insinuating that you don't know the answer Anyways, the discussion board would be better off without such insinuations!
So, how do you use try/catch blocks in a destructor
It's probably a trick question.
You don't use try/catch blocks in a destructor in C# because you shouldn't be writing a destructor. If you think you should be writing a destructor, you're probably wrong. If you think you should be writing a destructor and you're right, you already know that any exception thrown by a destructor cannot be caught and will crash the program, so you have to ensure that your destructor catches and handles any exceptions that it might throw.
But it's pretty hard to come up with a reasonable use case for C# destructors. It's exceptionally rare that both of the following are true:
This is almost never the case. I learned this myself when writing a class that wrapped around a horrible legacy API which required that a specific function be called when the series of API calls was complete before a new series could be started. That seemed like a perfect place to use a destructor.
Then my program started randomly crashing, and it was because the destructor (and thus the close-API function) wasn't actually getting called until the garbage collector finalized the object, and the new object was encountering the API in the wrong state. An especially hard problem to debug, since at its core it's a race condition, so (for instance) the code functions differently when stepped through in the debugger than it does when running.
It's vastly more likely that you want your object to release resources when it's disposed, that you should be implementing IDisposable, and that code that instantiates your object should do so in a using block.