I am someone migrating from a C++/Win32 development environment to C#/.NET. One thing that I have noticed is that mostly erroneous states are handled in C++/Win32 by the use of error codes and their propagation.
To the contrary most erroneous states in C# /.NET seem to be handled by the use of exceptions and error codes are seldom advised to be used.
Why so?
The main issue with error codes is that one needs to check them. Always.
And we are only human and can forget.
This can mean we can get our programs into inconsistent state by simply forgetting to check an error code.
If we forget to handle an exception, though, our program will exit.
This is seen as preferable to it continuing to run in an inconsistent state.
Much of the reason that error codes are still prevalent in C/C++ code is historical – these languages didn’t have exception handling, so needed error codes. And there are many libraries and code out there that conforms to this idiom, so programmers need to keep with it.
There are other reasons to use exception apart from the fact that they cannot be ignored – they carry quite a lot of context, as Marc observed – stack traces, messages and more, beyond the type of the exception.