What is the purpose of having so many types of exceptions in .net? Why not just use Exception?
Some of them are EndOfStreamException, FileLoadException, FileNotFoundException, IOException, InvalidTimeZoneException......
What is the purpose of having so many types of exceptions in .net? Why not just use Exception?
Some of them are EndOfStreamException, FileLoadException, FileNotFoundException, IOException, InvalidTimeZoneException......
Because you might want to catch one of them. You can't selectively catch an exception if they are all raised as Exception.
The individual exception types allow more information about the exception, and what caused it, to be passed.
For example, a FileNotFoundException tells you exactly why you received the exception, and lets you look at properties of the exception like which filename caused the failure, etc.
WIth a general "Exception" class, you'd only know "something went wrong", but not what specifically went wrong. By using concrete types, you have more flexibility to handle the exception and recover gracefully.