简体   繁体   English

异常处理

[英]Exceptions handle

I need to know witch of this two handling of exceptions is better and why??我需要知道这两种处理异常的女巫更好,为什么? Or some other way to do better.或者其他一些做得更好的方法。

try{
                if (String.IsNullOrEmpty(filePath))
                {
                    throw new ArgumentNullException("The path is null or empty.", "filePath");
                }

or或者

 try{
                if (String.IsNullOrEmpty(filePath))
                {
                    Console.WriteLine("The path is null or empty");
                } 

I would recommend the second approach, except that you don't need the try/catch in this case.我会推荐第二种方法,除了在这种情况下你不需要 try/catch。 Exceptions are for handling exceptional cases. Exceptions 用于处理异常情况。 If you can prevent those cases with an if condition you should prefer this.如果您可以使用if条件防止这些情况,您应该更喜欢这种情况。 If you are some performance optimization maniac you probably already know that throwing and catching exceptions could be more expensive compared to testing for conditions.如果您是一些性能优化狂,您可能已经知道与测试条件相比,抛出和捕获异常可能更昂贵。

If not having the path breaks your whole application flow, throwing an exception is a good idea so that you can recover the state of the application for another try, or show a dialog to the user.如果没有路径会破坏整个应用程序流程,那么抛出异常是一个好主意,这样您就可以恢复应用程序的 state 以再次尝试,或者向用户显示一个对话框。

On the other hand, Console.WriteLine("The path is null or empty");另一方面, Console.WriteLine("The path is null or empty"); just notifies the user of the situation and moves on.只是通知用户情况并继续前进。 If your execution flow is not harmed with, this will add minimal overhead to your application, thus preferable.如果您的执行流程没有受到损害,这将为您的应用程序增加最小的开销,因此更可取。

Use code contracts instead.改用代码合同 Especially for things like preconditions (ie, your example).特别是对于先决条件(即您的示例)之类的事情。

If the situation is an error to be expected, use return code or similar.如果情况是预期的错误,请使用返回码或类似的。 Like a file not existing or such像不存在的文件或类似的文件

If it is an unexpected situation (null as an argument ) use an excpetion.如果是意外情况(null 作为参数),请使用 excpetion。

Exception handling is expensive, so use wisely.异常处理是昂贵的,所以要明智地使用。

hth hth

Mario马里奥

Well, first of all in the second code you don't throw any exception, you simply write a message to the console.好吧,首先在第二个代码中你不会抛出任何异常,你只需向控制台写入一条消息。 This means that the second one won't signal anything in a windows forms or wpf application or - worse - a web application.这意味着第二个不会在 windows forms 或 wpf 应用程序或更糟糕的 Z2567A5EC9705EB7ACDZ 应用程序中发出任何信号。 Besides that, by throwing an exception you are able to intercept it on another stage of the application, and behaving correspondingly, whereas Console.WriteLine is only specific to the scope where you invoked it.除此之外,通过抛出异常,您可以在应用程序的另一个阶段拦截它,并采取相应的行为,而 Console.WriteLine 仅特定于您调用它的 scope。

You might also want to consider whether throwing an exception is appropriate at all.您可能还想考虑抛出异常是否合适。 This occurs in all cases where your code could gently handle the "exceptional" case, like by providing default values or such.这发生在您的代码可以温和地处理“异常”情况的所有情况下,例如通过提供默认值等。

I can't say this for sure because the rest of your program isn't there;我不能肯定地说,因为您的程序的 rest 不存在; but I'm guessing the first option is going to kill your program, while the second is going to just print something to the console and then continue with the rest of your program.但我猜第一个选项会杀死你的程序,而第二个选项只是在控制台上打印一些东西,然后继续你的程序的 rest。 So the answer to this question depends more on what YOU want it to do?所以这个问题的答案更多地取决于想要它做什么? Want the program to die when this situation happens?当这种情况发生时,想要程序死掉吗? Throw the exception and don't handle it.抛出异常并且不处理它。 Want to reprompt the user when this happens?想要在发生这种情况时重新提示用户? Then use your second option, but turn your IF statement into a while loop, and just prompt until the condition is no longer true.然后使用您的第二个选项,但将您的 IF 语句变成一个 while 循环,并一直提示直到条件不再为真。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM