August 2017
Beginner
566 pages
12h 34m
English
Okay, let's catch the exception! Change the first line of our catch block to the following:
catch(DivideByZeroException ex)
Remember that DivideByZeroException is a class, and here we are creating an object from this class, called ex. This is the usual name for an exception object. This object carries information, so let's take a look at how we can access it. Within the catch block, type the following:
Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
Here, we're using WriteLine to print our error message to the screen; then, on a new line, we're printing StackTrace. The good thing about these kinds of errors is that ex is an object of the type DivideByZeroException, and it carries this useful information. So, ...
Read now
Unlock full access