Docsity

Exception Handling - C Sharp Programming - Lecture Slides, Slides of C Sharp Programming

Partial preview of the text

Download Exception Handling - C Sharp Programming - Lecture Slides and more Slides C Sharp Programming in PDF only on Docsity!

Introduction to

Exception Handling and

Defensive Programming

Defensive Programming

(Introduction)

 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

Error Handling Strategy

 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

Exceptions

 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 Handling

(Introduction)

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

Exception Handling

(Key Concepts)

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 Exception Object

(Members)

 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

Exception Handling

C#

 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

Exception Handling

Syntax (2)

 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

Exception Handling

Details (1)

 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

Control Flow of Exceptions

Propagating Exceptions

 It‟s possible that the currently executing procedure has no exception handler  The call stack is searched for an “active” exception handler

Exception Handling Properties

 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

Exception Handling Example

 A simple exception handler

int Current; try { Current = System.Int32.MaxValue + 1 } Catch (System.Exception ex) { MessageBox.Show("Numeric overflow") }