C# – Learning about Exceptions

Exceptions are used when the program cannot move forward, such as receiving bad user input or running out of memory. They used be used as the name implies – in exceptional situations. Exceptions are not meant to used to alter the flow of the code.

The throw keyword is used to create an exception, which is an object. The throw keyword means that exception handling is type-safe because all exceptions are of a known type. Working with exceptions is also structured because you can structure the program to recover from an exception that occurs.

Exceptions can be recovered by using Exception Handlers in which the exception are then called handled exceptions. If there isn’t an exception handler to help the program recover from the exception then it’s referred to as an unhandled exception. If an unhandled exception occurs, that means the program will most likely force itself to shut down. This is to prevent the program from continuing to run in a possibly corrupt state.

Here is an example of using exceptions in the GradeBook program from previous posts.

On line 21, book.Name() changes the name of the book to an empty value ( also referred to as a null value ). As the program currently is, it will accept the value when it’s run.



This is because the book.Name property that handles the value doesn’t currently throw an exception to say “hey, this isn’t right!”. All that’s needed to add an exception at this point would be to add an if statement that says if the value is null or empty – to throw an exception.


(Please note that Name is a property within a class called GradeBook)

Lines 24-27 displays an if statement where the condition to evaluate is whether or not the value of the string entered is a null or empty value. Is the condition is true (it IS a null or empty value) then it throws a new exception object. The ArgumentException allows a customized text message to be displayed along side the stack trace when the exception is thrown.

When the program is run again with this in place, the results are a little bit different.


This time instead of saying that the gradebook had changed to “”, it throws an exception message and a stack trace. The message, being what it was set to, says “Name cannot be null or empty”. The message is then followed by the stack trace. The stack is a list of methods that are executed from one method until a value is returned. So you have a method, that calls another method, that calls another method, which leaves a trail behind aka the stack… when a value is finally returned the stack is scraped and a new one is created. At the bottom of the list is where the issue actually occurred and the trail follows upward. The stack trace here says that the error occurred on line 21 of program.cs which is where I did

book.Name = "";

This type of exception is an unhandled exception. The program makes no attempt to recover from this incorrect value being input. There is nothing to “catch” it the exception.

Will be following up with learning how to handle exceptions!



2 thoughts on “C# – Learning about Exceptions”

  1. Aye, the intent was to follow up this blog post with another that actually implemented a Catch statement in it. Unfortunately I ended being distracted by other things on my “To Learn” list and exception handling got put on the back burner. Currently working on CUITs which may eventually lead into exception handling.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s