What is the best alternative “On Error Resume Next” for C#?

后端 未结 11 2789
南方客
南方客 2021-02-19 08:12

If I put empty catch blocks for my C# code, is it going to be an equivalent for VB.NET\'s \"On Error Resume Next\" statement.

try
{
    C# code;
}

catch(excepti         


        
11条回答
  •  星月不相逢
    2021-02-19 08:49

    The proper .NET replacement for "on error resume next" is the use of Try___ methods. In Visual Basic 6.0, to find out if a key existed in a Collection, one had to either search the collection manually (horribly slow), or else try to index it and trap any error that occurred if it wasn't there. In VB.NET, the Dictionary object (which is an improved version of the old Collection) supports a TryGetValue method, which will indicate whether or not the attempt to get the value succeeded, without causing an error if it did not. A number of other .NET objects support similar functionality. There are a few methods which should have "try" equivalents but do not (for example, Control.BeginInvoke), but there are sufficiently few of them that wrapping them individually in a Try/Catch is not too onerous.

提交回复
热议问题