Presentation is loading. Please wait.

Presentation is loading. Please wait.

Debugging and Handling Exceptions

Similar presentations


Presentation on theme: "Debugging and Handling Exceptions"— Presentation transcript:

1 Debugging and Handling Exceptions
Chapter 11 Presented by Rodion Karmazin

2 !!Beforehand Warning!! None of the content in this PowerPoint belongs to me. All rights for the content go to their respective owners.

3 Quick Summary What you shall learn from this HUGE chapter are the “Try”, “Catch”, and “Finally” methods. Also you shall find out what debugging really is. I will also do my best to explain something about Exceptions

4 Error message does not always state the correct problem
Errors Visual Studio IDE reports errors as soon as it is able to detect a problem Syntax errors Language rule violation Error message does not always state the correct problem Quick info Figure 11-1 Syntax error – extraneous semicolon

5 Run-Time Errors Just because your program reports no syntax errors does not necessarily mean it is running correctly One form of run-time error is a logic error Program runs, but produces incorrect results May be off-by-one in a loop Sometime users enter incorrect values Finding the problem can be challenging

6 “Exceptions” Some circumstances are beyond programmer’s control
You have assumed nothing unusual would occur Have probably experienced unhandled exceptions being thrown While you browsed Web pages Unless provisions are made for handling exceptions, your program may crash or produce erroneous results Unhandled exception

7 “Exceptions” example Dialog box asks you whether you want to have an error report sent to Microsoft Figure 11-9 Microsoft error reporting

8 “Exceptions” example cont.
Message displayed when you are creating console application and unhandled exception occurs Figure Unhandled exception in a console application

9 “Unhandled Exceptions”
Selecting Debug>Start to run application in Visual Studio Yellow arrow marks the error erroneous code highlighted Figure Unhandled exception thrown – dividing by zero

10 Raising an “Exception”
Error encountered – no recovery Raise or throw an exception Execution halts in the current method and the “Common Language Runtime” or “CLR” attempts to locate an “exception handler” Exception handler: block of code to be executed when a certain type of error occurs If no exception handler is found in current method, exception is thrown back to the calling method

11 Exception-Handling Techniques
If event creates a problem frequently, best to use conditional expressions to catch and fix problem Execution is slowed down when CLR has to halt a method and find an appropriate event handler Exception-handling techniques are for serious errors that occur infrequently Exceptions classes integrated within the FCL Used with the try…catch…finally program constructs .NET Framework Class Library

12 Try…Catch…Finally Blocks
Code that may create a problem is placed in the try block Code to deal with the problem (the exception handler) is placed in catch blocks Catch clause Code to be executed whether an exception is thrown or not is placed in the finally block

13 catch [ (ExceptionClassName exceptionIdentifier) ]
try { // Statements } catch [ (ExceptionClassName exceptionIdentifier) ] // Exception handler statements : // [additional catch clauses] [ finally } ] Notice square brackets indicate optional entry One catch clause required finally clause optional

14 Try…Catch…Finally Blocks (continued)
Generic catch clause Omit argument list with the catch Any exception thrown is handled by executing code within that catch block Control is never returned into the try block after an exception is thrown Using a try…catch block can keep the program from terminating abnormally

15 Use of Generic Catch Clause
This example uses a generic catch block Figure Generic catch block handles the exception

16 // calculate average grade for a class int totalGrades = 0; int grade = 0; int sumGrades = 0; String data = ""; Console.WriteLine("Please enter a grade [## to end]"); while (true) { try { data = Console.ReadLine(); grade = int.Parse(data); sumGrades += grade; totalGrades++; } catch (Exception e) if (data == "##") break; Console.WriteLine("Invalid data - try again"); Console.WriteLine("Please enter a grade [## to end]"); if (totalGrades > 0) Console.WriteLine("Average grade is {0:F2}", sumGrades / totalGrades); else Console.WriteLine("Average = 0 (No data was supplied)");

17 Comments, complaints, suggestions are also inquired for at this time!
Any Questions? Comments, complaints, suggestions are also inquired for at this time! !!!!!!!! READ THE BOOK

18 Assignments (5 Pointer) (10 Pointer)
Make a program that requests a phone number. If a knucklehead runs the program and types in a letter, display an error message. “Try” and “Catch” are required for this. (10 Pointer) Try to make out whatever is happening on slide 16 and make a program that asks for the number of scores and has the user put in the scores. If there is an error in the input, address that error immediately with a message box or Console.Writeline();


Download ppt "Debugging and Handling Exceptions"

Similar presentations


Ads by Google