|
Let's not forget that double clicking the error should take you to the exact line.
|
|
|
|
|
When using VS and all happens to be well, which could be a stretch.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
guys;
I was exminning some code and i found this:
try
{
try
{
...
}
finally
{
...
}
}
catch
{
throw;
}
I am wondering if this is legal. I mean catch anything and trow anything;
or maybe it's usefull for something. because the developer who write this code is someone i believe he is an expert.
Thank you;
Help people,so poeple can help you.
|
|
|
|
|
Guy ( or girl .. dunno )
A try is always followed by a catch section, hence the name try.catch block.
The sample you provided is legal, however I would add an additional catch block in case anything goes wrong in the second try section.
try
{
try
{
...
}
catch
{
}
finally
{
...
}
}
catch
{
throw;
}
|
|
|
|
|
Rick van Woudenberg wrote: A try is always followed by a catch section, hence the name try.catch block.
Incorrect. A try-finally construct is perfectly legal. It is only when a try block is followed by a catch block that they are called a try-catch construct.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
.. euhmm .. debatable I guess. Technically speaking you're right, I agree. But what's the use of a try block without a catch block. The try block contains the guarded code that may cause the exception. The block is executed until an exception is thrown or it is completed successfully. The catch clause can be used without arguments, in which case it catches any type of exception, and referred to as the general catch clause. It can also take an object argument derived from System.Exception, in which case it handles a specific exception. So, keeping all that in mind, I'm hard to convince why you'd want two or more try clauses with just one catch clause, as each try block is examined in order of importance and thus handled by the same catch clause.
.. or have I completely lost my mind again.
|
|
|
|
|
Rick van Woudenberg wrote: But what's the use of a try block without a catch block.
To ensure that the exception gets thrown up the chain, AND some critical piece of code gets executed. Consider this example:
private void DoSomething()
{
SqlConnection connection = null;
try
{
connection = new SqlConnection();
}
finally
{
if (connection.ConnectionState == ConnectionState.Open)
connection.Close();
}
} BTW, try/finally is exactly how the using statement is implemented for auto-disposable behaviour.
|
|
|
|
|
Pete,
Good point. I guess you're sample is way better than I used to do it. ( see code below ). Other than the obvious thread freezing and just the fact that handling exceptions are 'expensive'.
private void DoSomething()
{
SqlConnection connection = null;
try
{
connection = new SqlConnection();
connection.Open();
}
catch
{
MessageBox.Show("Connecting to the database failed..");
}
if (connection.ConnectionState == ConnectionState.Open)
connection.Close();
}
|
|
|
|
|
That code example made me shudder.
Separation of concerns; Is the concern database access or User notification, because it sure as hell shouldnt be both.
|
|
|
|
|
Well, as far as I'm concerned both would be nice. Sure, the connection to the database has priority over user notification, however when something stuffs up, I generally let the user know. In that particular case I would do something like :
private void DoSomething()
{
SqlConnection connection = null;
try
{
connection = new SqlConnection();
connection.Open();
}
catch(SqlException ex)
{
MessageBox.Show(ex.ToString();
}
finally
{
if (connection.ConnectionState == ConnectionState.Open)
connection.Close();
}
}
Then you actually have both of two worlds. However, that puts us right back to the essence of this discussion. Having a catch clause in a method is not something to be ashamed of, though I get the feeling that many developers think that way.
|
|
|
|
|
ahhh, no no no.
If your UI code is mixed with your database access code; you're doing it wrong
If you show Exception messages unsanitized to your users; you're doing it wrong
|
|
|
|
|
Totally agreed on the first point. But regarding the second, he did mention that he might put something else there.
I tend to have something which shows them the exception trace in the critical exception handler, because if there is a serious bug, you (the developer) want that information to fix it. In the case of a database connection failure, though, definitely not, because most of those reasons are not because your software is broken.
|
|
|
|
|
BobJanova wrote: you (the developer) want that information to fix it
And therein is the point. I want it, but I dont EVER want a user to see it. This is what the Windows Event Log (or, perhaps another type of log) is for.
You never have a reason to show a user an unsanitized exception message (caveat: they're programmers and can actually act on the information). There is the potential to give away sensitive information if you do so.
|
|
|
|
|
J4amieC wrote: This is what the Windows Event Log (or, perhaps another type of log) is for.
Or to put it another way - that's what log4net is for.
|
|
|
|
|
I totally agree with you, and I would never show an exception to a user. Hence the
, but I still think that you should at least say something when anything important messes up, like .. euhh .. a database connection that fails ?
I don't think it's wise to redirect general user messages that could be caused by an exception to the windows logs. Not very user friendly.
|
|
|
|
|
J4amieC wrote: If your UI code is mixed with your database access code; you're doing it
wrong If you show Exception messages unsanitized to your users;
you're doing it wrong
Ahhh, no. It depends upon the scope of the problem you're trying to solve. If this is a 1,000 line utility app that you're the only user for, this might be perfectly appropriate. If it's a 200,000 line client for a LOB app, then you might have issues.
Software Zen: delete this;
|
|
|
|
|
If you're in a helper layer such as a DAL then you don't want to add any code that will interact with the user, however you may want to do clean up before the exception is thrown up the chain to a level where the exception can be dealt with by the user. Equally what's the difference between the original example and the following?
Private Sub ExceptionMethod()
Try
DoExceptionCode()
Finally
DoCleanup()
End Try
End Sub
Private Sub ExceptionHandlerCode()
Try
ExceptionMethod()
Catch ex As Exception
ExceptionHandlerHere()
End Try
End Sub
|
|
|
|
|
Arrgggh - my eyes. The horror. Case insensitive code in our lovely curly bracketed case sensitive world.
|
|
|
|
|
Sorry, I'll use Smalltalk next time.
More seriously I had a vb editor open so it was just quicker to type it in there with the auto complete than to start up a new c# editor for the example (formatting purposes)
|
|
|
|
|
DragonLord66 wrote: I had a vb editor open
In the name of all that's holy man, why?
|
|
|
|
|
We have a very large code base of VB code that was written simply because it's easier to get a working prototype in vb (pre 2010 and c# runtime code editing), and the prototypes turned into production code...
|
|
|
|
|
Oh come on, its just another way of saying (explaining) the same thing.
|
|
|
|
|
Suppose the line
connection = new SqlConnection();
fails and connection stays null.
Won't you receive a very ugly "unhandled exception" message? (haven't tried it).
V.
|
|
|
|
|
You will, but the exception will bubble up (actually, in this case you won't get an exception. The zero parameter constructor doesn't do anything that can cause an exception). I deliberately didn't put exception handling in here to avoid clouding the issue.
|
|
|
|
|
that is perfectly legal. The outer try-catch will catch whatever gets thrown outside the inner try block, e.g. an exception occurring in the inner finally block.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|