"GiJeet" <gi****@yahoo.c omwrote in message
news:fe******** *************** ***********@p59 g2000hsd.google groups.com...
Hello, I come from the VB6 world where we'd put a single ON ERROR GOTO
ErrHandler at the top of a method. Now whenever an error happened it
would drop into the ErrHandler code. In .Net it seems like you have
too many Try/Catch statements all throughout the code. I think this
is ugly error handling.
Is there a better way to handle exceptions in .Net 2.0 + then putting
all these Try/Catch blocks around code?
Not quite sure why you would get loads of Try Catches
In VB6:-
Function DoSomething() As Long
On Error Goto An_Error
'Code here
The_Exit:
'Tidy up
Exit Function
An_Error:
'Handle error
'optionally
Resume The_Exit
'or re-raise error not handled
' Duplicated Tidy up code
Err.Raise num, source, description
End Function
In C#:-
int DoSomething()
{
try
{
//code here
}
catch(SpecialEx ception e) //optional
{
//code to handle special exception
}
catch(Exception e) //(Exception e) can be deleted it e not used
{
//code to handle general exception
}
finally
{
//Tidy up; this always runs.
}
}
The point is there is no need for multiple Try/Catch blocks any more than
there is a need for multiple On Error Resume Handler lines in a VB6
procedure.
--
Anthony Jones - MVP ASP/ASP.NET