开发者

Preferred way of checking for errors

开发者 https://www.devze.com 2023-01-28 10:52 出处:网络
If I want to say, check that an int is between two certain numbers, would it be better to implement it without exceptions like so:

If I want to say, check that an int is between two certain numbers, would it be better to implement it without exceptions like so:

int a; //example in C++, but translate into your language of choice...
cin >> a;
if(a < 0 || a > 5)
    cout << "You must enter in a number between 0 and 5 inclusive!" << endl;

Or wit开发者_Python百科h exception in a try-catch block?

try
{
    int a;
    cin >> a;
    if(a < 0 || a > 5)
        throw "You must enter in a number between 0 and 5 inclusive!" << endl;
}
catch(string msg)
{
    cout << msg << endl;
}


Neither is 'better'. How to deal with anomalous conditions, errors, etc. totally depends on the exact circumstances and design of your code. Try-catch blocks are great for when an error should cause the following code to be skipped. On the other hand, when you want to let the user know about the problem but continue anyways by, say, assuming a default value, then try-catch may not be appropriate.

Additionally, some programs are designed with custom error-handling code so that errors and anomalies can be logged and recorded, and to ensure the program can fail gracefully. If you are working with that kind of system, you may not be using 'throw' at all, but instead something like ErrorHandler.logError("Something went wrong!");


The question is 'Can the function decide how to handle bad/invalid/out of bounds input?'. If it can than no error should be raised, else raise an error.

'Is this something exceptional?'. Disks filling up and causing filesystem errors can be hard to know how to deal with. Bad user input may not be so unusual and can probably be handled through regular code.

Exceptions are an extremely powerful tools to get code back on track. However, they break up code flow and can make the regular program logic less readable.

Normally an exception would peculate up to the caller function. In the example you give, you immediately catch your own error - so there is not much point to throw an error. You could just as easily print the line and be done with it.

In the example above it would seem like the function should be able to handle input errors, since it is also asking for the data and knows the bounds of the data. But that may not always be true, and depends on design and intention also.


Depends on your goals, and depends on the language (you listed this as language agnostic.) In C++ exceptions are pretty expensive, so you should be careful how you use them. My rule is that if the error is user visible then you can use an exception. However, many people will disagree. Basically there are a number of different schools of thought.

In the C# world, where I mainly work, Exceptions are very inexpensive, and so that can be used much more widely, for a much wider range of error handling.

In the particular example you give, it depends on what might cause the error. If it is user validation error (as it seems to be) then an exception would be appropriate. If it is a normal part of the program flow, then return codes would be more appropriate.

0

精彩评论

暂无评论...
验证码 换一张
取 消