recherche

Hi!

I want to know, under what circumstances you use try/catch blocks in constructors and destructors Example

Thanks



Re: Visual C# Language C#, try/catch blocks

Robert Rossney

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.





Re: Visual C# Language C#, try/catch blocks

Rini_Bhavsar_dd1aa1

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;

}





Re: Visual C# Language C#, try/catch blocks

recherche

Hi!

Thanks for the reply! But when do you use try/catch blocks indestructors Example





Re: Visual C# Language C#, try/catch blocks

Robert Rossney

Why would you use a destructor





Re: Visual C# Language C#, try/catch blocks

Nimrand

Finalizers (i.e., destructors) are uncommon in C#. It is probably even less common to use exceptions in them, as you are rarely doing anything that could cause an exception. But, if you were to use try/catch, you'd use it the same way as you would anywhere else and for the same reasons. Why do you need a specific example of using try/catch in a destructor If you're trying to learn C#, you should focus on the concepts of using exceptions and try/catch blocks, which really has very little to do with where they are used (constructors, destructors, methods, or properties).




Re: Visual C# Language C#, try/catch blocks

recherche

Hi!

Actually, I was asked to give an example of using try/catch in a destructor at an Accenture interview!

Thanks





Re: Visual C# Language C#, try/catch blocks

Nimrand

Given your many posts and the way you've insisted on others giving very specific kinds of examples, I suspected something along that lines. Asking a thoughtful questions to fill in some of the gaps of your knowledge pertaining to a test or interview would be one thing. But, it seems you've been asking others on this board to answer your interview questions for you, which means you are either unwilling to put the effort of applying what you do know to come up with your own answers, or you are trying to pass yourself off as knowing a lot more about C# programming than you actually do by giving canned answers that others have given you. Either way, I don't see it as a constructive use of these discussion boards.




Re: Visual C# Language C#, try/catch blocks

recherche

Are you insinuating that you don't know the answer Anyways, the discussion board would be better off without such insinuations!





Re: Visual C# Language C#, try/catch blocks

Robert Rossney

It's certainly not necessary to insinuate what is evident.





Re: Visual C# Language C#, try/catch blocks

recherche

So, how do you use try/catch blocks in a destructor





Re: Visual C# Language C#, try/catch blocks

Robert Rossney

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:

  1. You need your object to release some kind of unmanaged resource when it's destroyed.
  2. It's OK if this happens nondeterministically, whenever the garbage collector gets around to finalizing the object.

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.





Re: Visual C# Language C#, try/catch blocks

recherche

That's highly informative! Thanks indeed.