What is the purpose of exceptions?

Could you explain the concept of exceptions in C #?

+4
source share
2 answers

An exception is when a method or function cannot do what it should do.

If the OpenFile method cannot open the file [for whatever reason] and return the file descriptor to the caller, this is an exception to the OpenFile method, since it cannot open the file, which is its main purpose. As such, there is nothing like an exception, which may be the exception for you, it may be chronic for someone else in some other context . We can call it Execution Failures . The main purpose of exceptions is to report errors.

Example: OutOfMemoryException may be an exception condition for ordinary applications for entering small-scale data, but not for those applications that themselves use memory management, for example SqlServer or IIS. Consider the case of reading a stream. If the ReadByte method reaches the end of the stream and no more bytes are read, while you call ReadByte in the stream, it should throw an exception because there are no more bytes to read. But when you call the ReadChar method at the end of the stream, it will read EOF, which is absolutely suitable for the ReadChar method, since EOF is a valid character to read. For a method condition, ReadByte is called as method-failure

+1
source

Check out the MSDN docs on Exceptions .

Simply put, an exception occurs whenever there is some error in the application. There are many different types of errors, such as array indexes that go out of bounds, errors in disk I / O, problems with a zero partition, etc. But in general, Exceptions arise when something happens that the program cannot automatically fix.

0
source

Source: https://habr.com/ru/post/1313822/


All Articles