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

后端 未结 11 2793
南方客
南方客 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:44

    Although sometimes this is acceptable, generally it indicates a code smell. If you're 100% sure you want to swallow the exception that has occurred you can do it the way you have, but generally if an exception is thrown you should do something.

    Generally you can achieve the same outcome with well designed code. If you're currently experiencing a specific error, add it to your question, but if you're asking just out of curiosity, no there isn't an equivalent, and that is a good thing.

    0 讨论(0)
  • 2021-02-19 08:44

    Although On Error Resume Next is certainly abused more than it's used legitimately, there are places where it would be helpful even in VB.NET.

    Consider a program which assigns values to a large number of Excel properties, such as defaults to all printer parameters -- there are a zillion printer parameters in Excel. Later versions of Excel might have properties which earlier versions don't support, and it isn't trivial to figure out which ones are supported in each version. The program should assign a value if the property exists but ignore the property if an older version of Excel is used.

    The "right" way to do this with VB.NET would be to determine which printer properties are supported by each version of Excel, read the version in use, and only assign to properties that are implemented in that version. That would require a lot of research and some code, all for little benefit. On Error Resume Next would be a more practical solution.

    And, unfortunately, I'm faced with precisely this problem now. The workaround I'm going to try is to write a subroutine which just assigns one value to another, ignoring errors. I'll call this subrouting in place of each assignment statement. Not too terible, but not so great either.

    0 讨论(0)
  • 2021-02-19 08:46

    I happen to think those people who invented "On Error Resume Next" did have something in mind when they created it. The answer to your question would be no, there's nothing equivalent to this construct in C#. We have in C# and .Net a lot of functions that are so hungry for care and attention it gets tiring after a while to cater to everybody's "exceptional behavior". When almost everything can throw an exception the word itself looses it's meaning somewhat. You're inside an iteration and what should you do if few thousands of the million items happen to be exceptional ? Resume Next could be one of the handy answers.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 08:50

    You need to analyze the On Error Resume Next statements one by one and see what their purpose is. Some may be just sloppy code, but there are valid reasons for On Error Resume Next in Visual Basic 6.0 code.

    Some examples of why to use On Error Resume Next in Visual Basic 6.0 code:

    • To check if a given key exists in a Visual Basic 6.0 collection. The only way to do this is to access the element by key, and handle the error that is raised if the key does not exist. When converting to .NET, you can replace this by a check for the existence of the key.

    • Parsing a string to an integer. In .NET you can use TryParse.

    0 讨论(0)
提交回复
热议问题