1 / 13

Understand Error Handling

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

betha
Download Presentation

Understand Error Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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. Guiding Questions How do you handle an exception? How do you throw an exception and under what conditions?

  4. Activator–What Causes These Exceptions? DivideByZeroException IndexOutOfRangeException NullReferenceException StackOverflowException

  5. 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. 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. 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. 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. 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. 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. Example 3: Throwing an Exception static void CopyObject(SampleClass original) { if (original == null) { throw new ArgumentException("Parameter cannot be null", "original"); } }

  12. 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. 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?

More Related