tags:

views:

89

answers:

2

Could you explain the concept of exceptions in C#?

A: 

Check out the MSDN Docs on Exceptions.

Put simply, an Exception occurs whenever there is some error in the application. There are many different types of errors, such as array indexes going out of bounds, errors in disk I/O, Divide By Zero problems, etc. But in general, Exceptions occur whenever something happens that the program could not automatically correct for.

drharris
It's helpful to include some sort of summary here
Michael Mrozek
I did in the edit.
drharris
+1  A: 

Exception is a situation when a method or function cannot do what it is supposed to do.

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 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

this. __curious_geek
A method should do one of two things: Return a value representing the result of the requested operation, or throw an exception if it cannot complete the requested operation.
kyoryu
error-code based error reporting is not at all advisable. The method must serve upto its purpose and return only the acceptable values. Any condition that prevents the method from doing so is exception and should `HResult` which can be used to relate the error with external bug-database or documentation. This is so because you only wish to inform about the error-condition and not how to solve one.
this. __curious_geek
please justify the down vote.
this. __curious_geek