10266A_04.ppt
- Количество слайдов: 17
Module 4 Handling Exceptions
Module Overview • Handling Exceptions • Raising Exceptions
Lesson 1: Handling Exceptions • What Is an Exception? • Using a Try/Catch Block • Using Exception Properties • Using a Finally Block • Using the Checked and Unchecked Keywords • Demonstration: Raising Exceptions in Visual Studio
What Is an Exception? An exception is an indication of an error or exceptional condition, such as trying to open a file that does not exist When a method throws an exception, the calling code must be prepared to detect and handle the exception If the calling code cannot handle the exception, the exception is automatically propagated to the code that invoked the calling code The exception is propagated until a section of code handles the exception If no code handles the exception, the runtime will report an unhandled exception and the application will crash
Using a Try/Catch Block Logic that might cause an exception try { // Try block. Logic to run in the event of a Divide. By. Zero. Exception exception } catch (Divide. By. Zero. Exception ex) { // Catch block, can access Divide. By. Zero. Exception // exception in ex. Logic to run in the event } of any other exceptions catch (Exception ex) { // Catch block, can access exception in ex. } You can also nest try/catch blocks Exception types can implement a hierarchy of exceptions
Using Exception Properties Common properties include: Target. Site Message Inner. Exception Source Help. Link Stack. Trace Data try { // Try block. } catch (Divide. By. Zero. Exception ex) { Console. Write. Line(ex. Message); } Get the message associated with the exception
Using a Finally Block Enables you to release resources and specify code that will always run, whether an exception occurs or not try { Open. File("My. File"); // Open a file Write. To. File(. . . ); // Write some data to the file } catch (IOException ex) { Message. Box. Show(ex. Message); } finally { Close. File("My. File"); // Close the file } Logic that will always run
Using the Checked and Unchecked Keywords C# applications run with integer numeric overflow checking disabled by default. You can change this project setting You can control overflow checking for specific code by using the checked and unchecked keywords checked { int x =. . . ; int y =. . . ; int z =. . . ; . . . } unchecked { int x =. . . ; checked and int y =. . . ; int z =. . . ; unchecked blocks. . . } . . . int z = checked(x* y); . . . checked and . . . unchecked int z = unchecked(x* y); . . . operators
Demonstration: Raising Exceptions in Visual Studio In this demonstration, you will do the following: • Open the existing application and view the existing code • Run the application and examine how it currently handles exceptions • Modify the exception configuration in Visual Studio to always throw exceptions • Rerun the application and examine the different behavior
Lesson 2: Raising Exceptions • Creating an Exception Object • Throwing an Exception • Best Practices for Handling and Raising Exceptions
Creating an Exception Object System. Exception System. Format. Exception System. Argument. Exception System. Not. Supported. Exception + many more catch (Exception e) { Format. Exception ex = new Format. Exception("Argument has the wrong format", e); }
Throwing an Exception throw keyword throw [exception object]; Exception object declaration Example public int Get. Integer. Root(int operand) { double root = Math. Sqrt(operand); if (root != (int)root) { throw new Argument. Exception("No integer root found. "); } return (int)root; }
Best Practices for Handling and Raising Exceptions "An error has occurred. If the problem persists, please contact an administrator…" "A connection to database Fabrikam. DB with password 'AB 12345' failed…"
Lab: Handling Exceptions • Exercise 1: Making a Method Fail-Safe • Exercise 2: Detecting an Exceptional Condition • Exercise 3: Checking for Numeric Overflow Logon information Virtual machine 10266 A-GEN-DEV User name Student Password Pa$$w 0 rd Estimated time: 60 minutes
Lab Scenario
Lab Review Questions • What construct did you use to make the method calls fail-safe? • What attribute did you need to decorate the test method with so that it expected an exception? • What keyword can you use to explicitly instruct the compiler or runtime to check for overflow exceptions?
Module Review and Takeaways • Review Questions • Best Practices