例外的目的是什么? [英] What is the Purpose of exceptions?

查看:95
本文介绍了例外的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能解释一下C#中的异常概念吗?

Could you explain the concept of exceptions in C#?

推荐答案


一种方法或函数无法执行
应该执行的操作的情况。

如果使用OpenFile方法无法打开文件(无论出于何种原因)并将文件句柄返回给调用者,这是OpenFile方法的例外,因为它无法打开文件,这是其主要目的。因此,没有什么例外,您的例外可能会在其他情况下对其他人长期存在。我们可以将其称为执行失败。异常的主要目的是传达错误情况。

If OpenFile method cannot open the file [for whatsoever reason] and return the file handle to caller, this is an exception for the OpenFile method because it could not open the file which is its primary purpose. As such there's nothing like exception, what may be an exception to you might be chronic to someone else in some different context. We can call it Execution Failures. Primary purpose of the exceptions is to communicate the error conditions.

Ex: OutOfMemoryException 可能是正常的小规模数据输入的例外条件应用程序,但不适用于那些自己执行内存管理的应用程序,例如SqlServer或IIS。考虑读取流的情况。如果 ReadByte 方法到达流的末尾,并且没有更多的字节可读取,那么当您在流上调用ReadByte时,它应该抛出异常,因为没有更多的字节了。要读取的字节。但是,当您在流的末尾调用 ReadChar 方法时,它将读取EOF,这对于ReadChar方法绝对是可以的,因为EOF是可以读取的有效字符。对于ReadByte方法,条件称为方法失败

Ex: OutOfMemoryException might be an exception condition for normal small scale data-entry applications but not for those applications that do their memory management on their own, like SqlServer or IIS. Consider a case of reading a stream. If the ReadByte method reaches to end of stream and there's no more byte to read, at that time when you call ReadByte on the stream it should thrown an exception because there are no more bytes to read. But when you call ReadChar method at the end of stream, It will read EOF which is absolutely ok for ReadChar method since EOF is a valid character to read. For ReadByte method condition is called as method-failure

这篇关于例外的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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