Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 420 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
try/catch + using, right syntax

#1
Which one:

using (var myObject = new MyClass())
{
try
{
// something here...
}
catch(Exception ex)
{
// Handle exception
}
}

OR

try
{
using (var myObject = new MyClass())
{
// something here...
}
}
catch(Exception ex)
{
// Handle exception
}
Reply

#2
Both are valid syntax. It really comes down to what you want to do: if you want to catch errors relating to creating/disposing the object, use the second. If not, use the first.
Reply

#3
There is one important thing which I'll call out here: The first one will **not** catch any exception arising out of calling the `MyClass` constructor.
Reply

#4
I prefer the second one. May as well trap errors relating to the creation of the object as well.
Reply

#5
If your catch statement needs to access the variable declared in a using statement, then inside is your only option.

If your catch statement needs the object referenced in the using before it is disposed, then inside is your only option.

If your catch statement takes an action of unknown duration, like displaying a message to the user, and you would like to dispose of your resources before that happens, then outside is your best option.

Whenever I have a scenerio similar to this, the try-catch block is usually in a different method further up the call stack from the using. It is not typical for a method to know how to handle exceptions that occur within it like this.

So my general recomendation is outside—way outside.

private void saveButton_Click(object sender, EventArgs args)
{
try
{
SaveFile(myFile); // The using statement will appear somewhere in here.
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
}
Reply

#6
It depends. If you are using Windows Communication Foundation (WCF), `using(...) { try... }` will not work correctly if the proxy in `using` statement is in exception state, i.e. Disposing this proxy will cause another exception.

Personally, I believe in minimal handling approach, i.e. handle only exception you are aware of at the point of execution. In other word, if you know that the initialization of a variable in `using` may throw a particular exception, I wrap it with `try-catch`. Similarly, if within `using` body something may happen, which is not directly related to the variable in `using`, then I wrap it with another `try` for that particular exception. I rarely use `Exception` in my `catch`es.

But I do like `IDisposable` and `using` though so I maybe biased.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through