2. Core Programming Understand computerstorage and data types Understand computerdecision structures Identify the appropriatemethod for handlingrepetition Understand errorhandling
3. Understand errorhandling Lesson Overview Students will understand error handling. In this lesson, you will learn: Structured exception handling using the try, catch, finally, and throw keywords
4. Understand errorhandling 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.
5. Understand errorhandling 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.
6. Understand errorhandling Example 1: try and catch intSafeDivision(int x, int y) { try { return (x / y); } catch (DivideByZeroExceptiondbz) { Console.WriteLine("Division by zero attempted!"); return 0; } }
7. Understand errorhandling 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 } }
8. Understand errorhandling Example 3: Throwing an Exception static void CopyObject(SampleClass original) { if (original == null) { throw new ArgumentException("Parameter cannot be null", "original"); } //Some code }
9.
10. Answer #1) DivideByZeroException int x = 100/0; #2) IndexOutOfRangeException int[] array = new int[3]; array[4] = 31; #3) NullReferenceException Object x; x.toString(); #4) StackOverflowException method4();