
























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

























Code Complete puts it nicely It‟s defensive driving. You are never sure what others will do We defend against others by Checking for invalid inputs
There is no one-size-fits-all solution Routines can return error codes We might shut down the program entirely Log and / or display messages
Closest legal value Next valid data item
Most languages work the similarly here Java, VB, C# It‟s a way a procedure can say “Something is wrong and I don‟t know what to do about it. I hope that you (caller) do.”
Exception - a run-time error that occurs as a result of some abnormal condition In Visual Studio, a dialog box appears in cases of an unhandled exception
Catching an expression or handling an exception - when an exception occurs, statements execute to process the exception in some way Throwing an exception - a component may generate an exception that will be caught by another component
The ToString method converts the error message to a string The Message property stores the same message The StackTrace property shows where the exception occurred and the call stack This is the same call stack that we discussed during the debugging lecture
C# uses the try , catch , and finally , blocks to build structured exception handlers Structured exception handlers are just a specialized form of a decision-making statement They are really just a form of switch statement
Syntax (cont) [ catch exception name ] Code that executes when an exception occurs in the Try block [ finally ] Statements that always execute regardless of whether an exception occurs
Statements following exception handler
The statements in the try block contain statements that could cause an exception to be thrown If an exception is thrown, the statements in a catch block execute An exception handler may have multiple catch blocks to handle different types of exceptions
It‟s possible that the currently executing procedure has no exception handler The call stack is searched for an “active” exception handler
All exceptions share similar properties InnerException property allows the application to store the exception that led up to the current exception Message property contains a textual message describing the exception Source property gets the name of the application or object that caused the exception to occur
A simple exception handler
int Current; try { Current = System.Int32.MaxValue + 1 } Catch (System.Exception ex) { MessageBox.Show("Numeric overflow") }