1.4 core programming [understand error handling]

1,406 views

Published on

Published in: Education
  • Be the first to comment

1.4 core programming [understand error handling]

  1. 1. Core Programming<br />นายสมเกียรติ สอนนวล<br />Cimatt Business Group Co.,LTD.<br />
  2. 2. Core Programming<br />Understand computerstorage and data types<br />Understand computerdecision structures<br />Identify the appropriatemethod for handlingrepetition<br />Understand errorhandling<br />
  3. 3. Understand errorhandling<br />Lesson Overview<br />Students will understand error handling. In this lesson, you will learn:<br />Structured exception handling using the try, catch, finally, and throw keywords<br />
  4. 4. Understand errorhandling<br />Review Term<br />Exception<br />A problem or change in conditions that causes the microprocessor to stop what it is doing and handle the situation in a separate routine.<br />
  5. 5. Understand errorhandling<br />How to Handle Exceptions<br />A try block is used by C# programmers to partition code that may be affected by an exception.<br />A catch block is used to handle any resulting exceptions.<br />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.<br />A try block must be used with either a catch or a finally block, and it can include multiple catch blocks.<br />
  6. 6. Understand errorhandling<br />Example 1: try and catch<br />intSafeDivision(int x, int y)<br />{<br />try<br />{<br /> return (x / y);<br />}<br />catch (DivideByZeroExceptiondbz)<br />{<br />Console.WriteLine("Division by zero attempted!");<br />return 0;<br />}<br />}<br />
  7. 7. Understand errorhandling<br />Example 2: try, catch, and finally<br />static void Main()<br />{<br />try<br />{<br /> //statement which can cause exception<br />}<br />catch (Exception e)<br />{<br /> //statement for handling exception<br />}<br />finally<br />{<br /> //cleanup code<br />}<br />}<br />
  8. 8. Understand errorhandling<br />Example 3: Throwing an Exception<br />static void CopyObject(SampleClass original)<br />{<br />if (original == null)<br />{<br />throw new ArgumentException("Parameter cannot be null", "original");<br />}<br />//Some code<br />}<br />
  9. 9. Assignment<br />Provide the code in the try block that will trigger the exception handling in the catch block.<br />#1) static void method1()<br />{<br />try<br />{<br /> //Insert code here<br />}<br />catch (DivideByZeroException ex)<br />{<br />Console.WriteLine(ex.ToString());<br />}<br />}<br />#2) static void method2 ()<br />{<br />try<br />{<br />//Insert code here<br />}<br />catch (IndexOutOfRangeException ex)<br />{<br />Console.WriteLine(ex.ToString());<br />}<br />}<br /><ul><li>#3) static void method3 ()</li></ul>{<br />try<br />{<br />//Insert code here<br />}<br /> catch (NullReferenceException ex)<br />{<br />Console.WriteLine(ex.ToString());<br />}<br />}<br /><ul><li>#4) static void method4 ()</li></ul>{<br />try<br />{<br /> //Insert code here<br />}<br />catch (StackOverflowException ex)<br />{<br />Console.WriteLine(ex.ToString());<br />}<br />}<br />
  10. 10. Answer<br />#1) DivideByZeroException<br />int x = 100/0;<br />#2) IndexOutOfRangeException<br />int[] array = new int[3];<br /> array[4] = 31;<br />#3) NullReferenceException<br /> Object x;<br />x.toString();<br />#4) StackOverflowException<br /> method4();<br />

×