Your SlideShare is downloading. ×
1.4 core programming [understand error handling]
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

1.4 core programming [understand error handling]

937
views

Published on

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
937
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
52
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Core Programming
    นายสมเกียรติ สอนนวล
    Cimatt Business Group Co.,LTD.
  • 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. Assignment
    Provide the code in the try block that will trigger the exception handling in the catch block.
    #1) static void method1()
    {
    try
    {
    //Insert code here
    }
    catch (DivideByZeroException ex)
    {
    Console.WriteLine(ex.ToString());
    }
    }
    #2) static void method2 ()
    {
    try
    {
    //Insert code here
    }
    catch (IndexOutOfRangeException ex)
    {
    Console.WriteLine(ex.ToString());
    }
    }
    • #3) static void method3 ()
    {
    try
    {
    //Insert code here
    }
    catch (NullReferenceException ex)
    {
    Console.WriteLine(ex.ToString());
    }
    }
    • #4) static void method4 ()
    {
    try
    {
    //Insert code here
    }
    catch (StackOverflowException ex)
    {
    Console.WriteLine(ex.ToString());
    }
    }
  • 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();

×