Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understand Error Handling 98-361 Software Development Fundamentals LESSON 1.4.

Similar presentations


Presentation on theme: "Understand Error Handling 98-361 Software Development Fundamentals LESSON 1.4."— Presentation transcript:

1 Understand Error Handling 98-361 Software Development Fundamentals LESSON 1.4

2 98-361 Software Development Fundamentals LESSON 1.4 Lesson Overview Students will understand error handling. In this lesson, you will learn:  Structured exception handling using the try, catch, finally, and throw keywords

3 98-361 Software Development Fundamentals LESSON 1.4 Guiding Questions 1. How do you handle an exception? 2. How do you throw an exception and under what conditions?

4 98-361 Software Development Fundamentals LESSON 1.4 Activator–What Causes These Exceptions?  DivideByZeroException  IndexOutOfRangeException  NullReferenceException  StackOverflowException

5 98-361 Software Development Fundamentals LESSON 1.4 Review Term Exception  A problem or change in conditions that causes the microprocessor to stop what it is doing and handle the situation in a separate routine.  An exception is similar to an interrupt; both refer the microprocessor to a separate set of instructions.

6 98-361 Software Development Fundamentals LESSON 1.4 How to Handle Exceptions  A try block is used by C# programmers to partition code that may be affected by an exception.  A catch block is used to handle any resulting exceptions.  A finally block can be used to execute code regardless of whether an exception is thrown—which is sometimes necessary, as code following a try/catch construct will not be executed if an exception is thrown.  A try block must be used with either a catch or a finally block, and it can include multiple catch blocks.

7 98-361 Software Development Fundamentals LESSON 1.4 Example 1: try and catch int SafeDivision(int x, int y) { try { return (x / y); } catch (DivideByZeroException dbz) { Console.WriteLine("Division by zero attempted!"); return 0; } }

8 98-361 Software Development Fundamentals LESSON 1.4 Example 1: try and catch  When your application encounters an exceptional circumstance, such as a division by zero or a low memory warning, an exception is generated.  Use a try block around the statements that might throw exceptions.  Once an exception occurs within the try block, the flow of control immediately jumps to an associated exception handler, if one is present.  If no exception handler for a given exception is present, the exception is passed up the chain to the calling routine.

9 98-361 Software Development Fundamentals LESSON 1.4 Example 2: try, catch, and finally static void Main() { try { //statement which can cause exception } catch (Exception e) { //statement for handling exception } finally { //cleanup code } }

10 98-361 Software Development Fundamentals LESSON 1.4 Example 2: try, catch, and finally  A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.  Code in a finally block is executed even if an exception is thrown, thus allowing a program to release resources.  If no exception occurred inside the try block, the control directly transfers to the finally block.

11 98-361 Software Development Fundamentals LESSON 1.4 Example 3: Throwing an Exception static void CopyObject(SampleClass original) { if (original == null) { throw new ArgumentException("Parameter cannot be null", "original"); } }

12 98-361 Software Development Fundamentals LESSON 1.4 Example 3: Throwing an Exception  The method cannot complete its defined functionality.  In the previous case, the parameter to the method CopyObject has an invalid value.  Exception objects that describe an error are created and then thrown with the throw keyword.

13 98-361 Software Development Fundamentals LESSON 1.4 Lesson Review  What are some common exceptions and their causes?  How do you use a try-catch block to handle exceptions?  How do you use the throw keyword to indicate that an exception has occurred?


Download ppt "Understand Error Handling 98-361 Software Development Fundamentals LESSON 1.4."

Similar presentations


Ads by Google