130 likes | 326 Vues
Understand Error Handling. LESSON 1.4. 98-361 Software Development Fundamentals. Lesson Overview Students will understand error handling. In this lesson, you will learn: Structured exception handling using the try, catch, finally, and throw keywords. Guiding Questions
E N D
Understand Error Handling LESSON 1.4 98-361 Software Development Fundamentals
Lesson Overview • Students will understand error handling. • In this lesson, you will learn: • Structured exception handling using the try, catch, finally, and throw keywords
Guiding Questions How do you handle an exception? How do you throw an exception and under what conditions?
Activator–What Causes These Exceptions? DivideByZeroException IndexOutOfRangeException NullReferenceException StackOverflowException
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.
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.
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; } }
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.
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} }
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.
Example 3: Throwing an Exception static void CopyObject(SampleClass original) { if (original == null) { throw new ArgumentException("Parameter cannot be null", "original"); } }
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.
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?