Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
No matter how well you protect your code with exception handlers, errors can slip through your defenses. If you don't construct your own global handler, your application will use the Silverlight defaults. In this movie, I will show you how those defaults are configured, and how to implement your own error screens. For this demonstration, I will use Visual Studio and load a project called GlobalErrors. Let me show you the default UI before I introduce any exceptions. Press F5 to run the application and click on this Load vacation pictures, which loads a picture into the image control. My application is working as expected.
Let me introduce a bug into my code. I will go to my MainPage.xaml.cs file, and I am going to change the name of this file from a C to a K. So we'll say kabin.jpg. This should cause an exception when it attempts to load the stream. I am ready to test, but before I do, let's talk about the Debug menu. I have two choices for starting my app from Visual Studio: Start Debugging and Start Without Debugging. Now you may recall that when you choose Start Debugging, Visual Studio starts the Browser and then attaches a debugger to the browser.
So I can debug my code. That's important keep that in mind. Press Start Debugging. I will click in my button again. Visual Studio detects that I have an expression and gives me an opportunity to debug the exception. It does that by putting a yellow marker here on this line, stopping my code from running, and pulling up this little exception dialog. There is lots of information inside this dialog. If you click View Details, you can see some of them. For instance, I can see that it was a NullReferenceException. I can see some Data. I can click on the InnerException and see if there is any nested exceptions and Message and the StackTrace as well.
Now I can see the error in this bottom window. There it is. It was an Unhandled Error in Silverlight Application, the error code, and some other information about the error. Currently, I have no exception handling at the Global level inside my app. Let's change that. I will go to App.xaml.cs. Here inside my constructor, I have two sections where I could set up an exception handler. This is the standard Boilerplate code that Microsoft produces, and this is the custom code that I wrote.
Uncomment this line of code, and then I go look at my custom handler. Here it is. The first thing that I do in Customer Handler is I tell Silverlight to not let the error get to the Silverlight plug-in. I do that by saying e.Handled=True, e being this argument here, ApplicationUnhandledExceptionEventArgs. Next, I report my error to my own UI. What I am doing here is going to my main page, which is called the RootVisual. The RootVisual is the startup control inside your Silverlight application. Here you can see I am instantiating an instance of this class MainPage.xaml.
So in my custom exception handler, I am getting the RootVisual, I am calling a Custom method that I wrote call ShowErrorMessage, and I am passing in the exception. Now if I come over to my custom page and look at the code behind, you can see that the custom function that I wrote, ShowErrorMessage. Now what I am doing in here? I am creating my own UI, which is a child window called ErrorDialog, and then I am passing some information into my child window, and then I am showing that window. So that means of course if it created this ErrorDialog, here I can create my own beautiful UI for my exceptions and then on this TextBlock, I am going to load that in my code behind through this property. Time to see the application in action.
I will choose to Start Without Debugging, click on the button, and there is my custom error message. From my last demo, I thought we would look briefly at Microsoft's boilerplate code. Let's go over to App.xaml.cs. We'll scroll down to this section, which is a region that I put in my code. What Microsoft does in their boilerplate code is, inside this method, they check to see if there's a debugger attached, set the e.Handled=True and then do an asynchronous BeginInvoke call to a function they wrote called ReportErrorToDOM.
That function, which is down here, creates a new error message, and finally, it goes to the HTML page and throws a new error. The default handlers in Silverlight are a good start, but you can do better. In the next movie, you will see how to create custom loading screens in your application.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 97508 Viewers
61 Video lessons · 84747 Viewers
71 Video lessons · 68923 Viewers
56 Video lessons · 101377 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.