Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Now let's see some code examples to better showcase the exception handling in C#
The below is an exercise that currently, when run, will throw a
You should add the proper exception handling code so that the testing assertion can pass.
The below is an example of using multiple
catch blocks, in File reading program, where this will showcase how you should include the catch blocks from the most to least specific exceptions that might occur.
Note that we could have used a
finally block to dispose the
StreamReader objects, however, since we know that
StreamReader classes both implement the IDisposable Interface, then the cleaner and much better way to do through the using keyword.
Even if there is an exception raised from any of the above objects, the using keyword will dispose the opened stream object and the stream reader object.
Now in the below sample we include the keyword
throw to raise a new exception of type
MethodNotImplementedException, this exception is a custom created one, it inherits from the