简体   繁体   English

在.NET中检查和取消选中的异常

[英]checked and unchecked exception in .NET

a strange idea comes to me when I'm reading APUE(Advanced Programming in UNIX Environment). 当我在阅读APUE(UNIX环境中的高级编程)时,一个奇怪的想法来到我身边。

It seems that in UNIX's error handling, there are two types of errors (FATAL & INFATAL). 似乎在UNIX的错误处理中,有两种类型的错误(FATAL和INFATAL)。 I feel like it's something related to checked and unchecked Exceptions in JAVA. 我觉得这与JAVA中的已检查和未经检查的异常有关。

So, to sum up, in a program, you have two kinds of errors, one of them is critical and will make system crash and you can do nothing about it. 因此,总而言之,在一个程序中,您有两种错误,其中一种是关键的,会导致系统崩溃,而您却无能为力。 Another one is more like a signal that you can catch and do something to fix it. 另一个更像是一个信号,你可以抓住并做一些事情来解决它。

I heard that in C# there is no checked and unchecked exception, so does C# not have a concept of critical and uncritical errors? 我听说在C#中没有检查和未检查的异常,因此C#没有关键和非关键错误的概念吗? Just got very curious because i think this concept is very fundamental. 只是非常好奇,因为我认为这个概念非常基础。

Update: What is the exception design in other languages? 更新:其他语言的例外设计是什么? Can anyone talk about this? 任何人都可以谈论这个吗?

In Java, checked and unchecked exceptions don't exactly map to a fatal or non-fatal error. 在Java中,已检查和未检查的异常并不完全映射到致命或非致命错误。 A checked exception explicitly is stating that the exception may be thrown and someone must catch it (to try to handle it or throw it up the stack), but there is no guarantee that the error may not be fatal (ie a syntax error in an SQL query will throw an SQLException and will likely be fatal, but it is a checked exception). 显式检查异常明确表示可能抛出异常并且有人必须捕获它(尝试处理它或将其抛出堆栈),但不能保证错误可能不是致命的(即语法中的语法错误) SQL查询将抛出一个SQLException并且可能是致命的,但它是一个经过检查的异常)。 An unchecked exception just means that someone doesn't need to catch it, but you still can if you want. 未经检查的异常仅表示某人不需要捕获它,但如果您愿意,您仍然可以。 It would typically indicate a programming error. 它通常表示编程错误。 Java Errors typically indicate things that are unrecoverable problems (such as an OutOfMemoryError). Java错误通常表示不可恢复的问题(例如OutOfMemoryError)。

The C# design of unchecked exceptions just means you don't need to catch the exceptions, and if uncaught will crash the application. 未经检查的异常的C#设计意味着您不需要捕获异常,如果未被捕获将使应用程序崩溃。 Checked vs unchecked exceptions has been a long standing debate in the development community and there are pros and cons to both. 检查与未检查的异常一直是开发社区长期存在的争论,两者都有利有弊。 Typically though, you can't do something about an exception and it frequently just ends up getting logged rather than handled, so C# made exceptions unchecked. 但通常情况下,您无法对异常执行某些操作,并且它经常最终会被记录而不是处理,因此C#会将异常取消选中。 When you can handle them (for example, if you want to retry an IO operation), you can still catch them and retry. 当您可以处理它们时(例如,如果您想重试IO操作),您仍然可以捕获它们并重试。

I have, in previous systems that I no longer work on, created 2-level (fatal and non-fatal) exception systems in .NET -- mainly just by inheriting from the Exception class into two further base classes ( FatalException and NonFatalException ) and then deriving further, more specific Exception classes from these two. 在我之前不再使用的系统中,我在.NET中创建了2级(致命和非致命)异常系统 - 主要是通过从Exception类继承到另外两个基类( FatalExceptionNonFatalException )和然后从这两个派生更进一步,更具体的Exception类。 The fact that I no longer work this way demonstrates that I no longer feel this is necessary -- it just added to the ceremony of the system without adding much value. 我不再以这种方式工作的事实表明我不再觉得这是必要的 - 它只是增加了系统的仪式而没有增加太多价值。

IMHO ac# compiler built in exception class attribute like [CriticalException] vs. [NonCriticalException] would be nice. 在[CriticalException]与[NonCriticalException]之类的异常类属性中构建的IMHO ac#编译器会很好。 If you decorate your exception class with this attribute or (MS in the .NET runtime), the compiler would give you a warning, if no catch-block can be found somewhere in the call graph, assuming that in the call graph this specific exception is thrown. 如果使用此属性或(.NET运行时中的MS)装饰异常类,如果在调用图中的某处找不到catch-block,编译器会给出警告,假设在调用图中这个特定的异常被扔了。

Yes, there are such critical exceptions. 是的,有这样的关键例外。 For example, since version 2.0 of the framework, a StackOverflowException cannot be caught in a try-catch block 例如,从框架的2.0版开始,无法在try-catch块中捕获StackOverflowException

In C# there are special exceptions that you can't really recover from (I think all of them are related to memory issues) - but they are all system exceptions and you can't create a new one of these. 在C#中有一些特殊的异常,你无法真正从中恢复(我认为它们都与内存问题有关) - 但它们都是系统异常,你无法创建新的异常。

There is no mechanism to force the programmer to catch (or put declare throws ) like in Java - all exceptions would be classified as unchecked. 没有任何机制可以强制程序员像Java一样捕获(或声明throws ) - 所有异常都将归类为未选中。 Obviously you can still catch them and recover, but the compiler is not going to help you with this one. 显然你仍然可以捕获它们并恢复,但编译器不会帮助你解决这个问题。

In C#, you also have the ability to catch unhandled exceptions at an application level. 在C#中,您还可以在应用程序级别捕获未处理的异常。 These will be caught as errors (page error, application error), not exceptions, but you have access to the last exception so that you can at least log them. 这些将被捕获为错误(页面错误,应用程序错误),而不是异常,但您可以访问最后一个异常,以便至少可以记录它们。 At this point, you no longer have the option of fix-up and retry, but logging is crucial because your app is already going down. 此时,您不再可以选择修复和重试,但日志记录至关重要,因为您的应用程序已经关闭。

No idea what the equivalent last-ditch equivalent is for Java, or in Unix. 不知道Java或Unix中的等效最后一个等价物是什么。

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

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