C# FAQ: When should an exception be thrown

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:FAQs
edit

When should an exception be thrown?

There is no clear cut answer to the question of when to throw an exception. Rather, the answer is usually a matter of philosophy or style. But, many developers feel that exceptions should be thrown only when an unexpected error is encountered.

What is an unexpected error? There are many borderline cases; yet, a classic example of an expected error is failure to read from a file due to the the seek pointer being positioned at end of file. A classic example of an unexpected error is failing to allocate heap memory for an operation.


See also


Personal tools