.NET中的错误和异常之间有什么区别? [英] What is the difference between an error and an exception in .NET?

查看:88
本文介绍了.NET中的错误和异常之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否请您向我解释错误和异常之间的区别是什么?

Could you please explain to me what the difference is between an error and an exception?

推荐答案

异常是一个类利用语言语义。正如其他人所述,异常会中断堆栈的执行,直到被捕获为止。可以使用异常 来传达错误,但更普遍的是,它可以用来传达发生了异常情况。

An exception is a class that takes advantage of language semantics. As others have stated, exceptions interrupt execution up the stack until caught. An exception can be used to convey an error, but more generally is used to convey that something exceptional has occurred.

错误,另一方面

有几种错误:


  • 用户错误-应无例外处理

  • 语法错误-不应以静态类型的语言进行编译(在动态语言中,很难发现它们)

  • 运行时错误-这将导致异常或无声地失败(通常会产生意外的结果)

由于用户输入的错误数据不是异常,因此异常应仅限于处理运行时错误。要处理用户错误,您应该采用以下方法:

Really, exceptions should be limited to handling runtime errors, since a user inputting bad data is not "exceptional." To handle user errors, you should take the following approaches:


  • 防止输入不良数据(前端验证)

  • 防止不良数据被保留(后端验证)

应将异常用作最后一个用户错误的防线。如果要编写持久层,则可以依靠异常来确保不会丢失通过验证的不良数据。但是,您应该通过在验证中添加一个修复程序来修复所有这些错误,以防止错误首先出现。

Exceptions should be used as a "last line of defense" for user error. If you're writing a persistence layer, you can rely on exceptions to ensure that bad data that falls through validation does not get persisted. You should, however, fix any of these by putting a fix in the validation that prevents the error from occurring in the first place.

这篇关于.NET中的错误和异常之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆