Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exception Handling CSCI293 - C# October 3, 2005.

Similar presentations


Presentation on theme: "Exception Handling CSCI293 - C# October 3, 2005."— Presentation transcript:

1 Exception Handling CSCI293 - C# October 3, 2005

2 Definition An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program.

3 How Exceptions are Processed
Suppose that main calls function A which calls function B: If a statement in B causes an exception and that statement is not inside a try block, then function B throws the exception up to function A. If A does not handle the exception, then the exception is thrown up to main. If main throws an exception, the program crashes.

4 static void Main(string[] args)
{ Console.WriteLine("inside main"); func1(); Console.WriteLine("leaving main"); } static void func1() Console.WriteLine("inside func1"); func2(); Console.WriteLine("leaving func1"); static void func2() Console.WriteLine("inside func2"); int a = 0, b=9, c; c = b/a; Console.WriteLine("leaving func2");

5 Try Statement try catch { code that may have a problem }
"Do, or do not. There is no 'try'". -- Yoda try { code that may have a problem } catch what to do if error occurs

6 static void Main(string[] args)
{ Console.WriteLine("inside main"); func1(); Console.WriteLine("leaving main"); } static void func1() Console.WriteLine("inside func1"); try func2(); catch Console.WriteLine("func1 caught an execption"); Console.WriteLine("leaving func1"); static void func2() Console.WriteLine("inside func2"); int a = 0, b=9, c; c = b/a; Console.WriteLine("leaving func2");

7 If at first you don’t succeed, try, try again...
{ func2(); } catch (System.ArithmeticException e) Console.WriteLine("func1 - " + e.Message); catch Console.WriteLine("func1 - unknown exception

8 Create your own... Exceptions are objects, like everything else:
public class MyException : System.ApplicationException { public MyException (string msg) base (msg) }

9 Intentionally Causing Problems
Assuming errorHasOccurred is one of our variables... if (errorHasOccurred) throw new MyException();

10 int[] list = new int[10]; string inputline; int intvalue = 0; Console.WriteLine("Enter ten integers"); for (int i=0; i<10; i++) { inputline = Console.ReadLine(); try intvalue = Convert.ToInt32(inputline); } catch { } list[i] = intvalue; Console.WriteLine("bob contains: "); foreach (int x in list) Console.Write("{0} ", x); Console.WriteLine();

11 int[] list = new int[10]; string inputline; int intvalue = 0; Console.WriteLine("Enter ten integers"); for (int i=0; i<10; i++) try { inputline = Console.ReadLine(); intvalue = Convert.ToInt32(inputline); list[i] = intvalue; } catch { } Console.WriteLine("bob contains: "); foreach (int x in list) Console.Write("{0} ", x); Console.WriteLine();


Download ppt "Exception Handling CSCI293 - C# October 3, 2005."

Similar presentations


Ads by Google