chapter 13 exception handling: a deeper look

43
Chapter 13 Exception Handling: A Deeper Look 1

Upload: nicholas-carroll

Post on 31-Dec-2015

41 views

Category:

Documents


1 download

DESCRIPTION

Chapter 13 Exception Handling: A Deeper Look. Things Happen. Terminology. Exception Keywords. Running example. using System; using System.Collections.Generic ; using System.Linq ; using System.Text ; namespace UsingExceptions { class Program { - PowerPoint PPT Presentation

TRANSCRIPT

Chapter 13Exception Handling:

A Deeper Look

1

Things Happen

2

Terminology

3

Exception Keywords

4

Running exampleusing System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace UsingExceptions

{

class Program

{

static void Main(string[] args)

{

int x = 10;

int y = 5;

int result;

try

{

result = x / y;

Console.WriteLine("The result is: {0}", result);

}

catch

{

Console.WriteLine("An error occurred! Better check the code!");

}

finally

{

Console.WriteLine("Just proving that this code always runs.");

}

Console.ReadLine();

}}}

5

Catching Specific Exceptions

6

Hierarchy

try

{

result = x / y;

Console.WriteLine("The result is: {0}", result);

}

catch (System.DivideByZeroException e)

{

Console.WriteLine("Whoops! You tried to divide by zero!");

Console.WriteLine(e.Message);

}

catch (System.ArithmeticException e)

{

Console.WriteLine("Whoops! You tried to divide by zero!");

Console.WriteLine(e.Message);

}

7

More Specific First try

{

result = x / y;

Console.WriteLine("The result is: {0}", result);

}

catch (System.ArithmeticException e)

{

Console.WriteLine("Whoops! You tried to divide by zero!");

Console.WriteLine(e.Message);

}

catch (System.DivideByZeroException e)

{

Console.WriteLine("Whoops! You tried to divide by zero!");

Console.WriteLine(e.Message);

}

finally

{

Console.WriteLine("Just proving that this code always runs.");

}

Console.ReadLine();

8

Exceptions are Objects

System.Exception Constructors Properties Hierarchy

http://msdn.microsoft.com/en-us/library/system.systemexception.aspx

System.Exception◦ System.ApplicationException (non fatal exceptions)◦ http://msdn.microsoft.com/en-us/library/system.applicationexception.aspx

9

Custom Exceptions

10

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace CustomExceptions

{

public class MyOwnException : Exception

{

public MyOwnException()

: base("Sergey is not welcomed here!") //Exception(string)

{

//this.HelpLink = "http://msdn.microsoft.com/en-us/library/system.exception.aspx";

}

}

class Program

{

static string GetName()

{

string s = Console.ReadLine();

if (s.Equals("Sergey"))

throw new MyOwnException();

return s;

}

11

static void Main(string[] args)

{

string theName;

try

{

theName = GetName();

Console.WriteLine("Hello {0}", theName);

}

catch (MyOwnException nje)

{

Console.WriteLine(nje.Message);

Console.WriteLine("For help, visit: {0}", nje.HelpLink);

}

finally

{

Console.WriteLine("Have a nice day!");

}

Console.ReadLine();

}

}

}

12

13.2 Example: Divide by Zero without Exception Handling

13

Re-throwing Exceptions

class Program

{

static void DoSomeMath()

{

int x = 10, y = 0;

int result;

try

{

result = x / y;

Console.WriteLine("Result is {0}", result);

}

catch

{

Console.WriteLine("Error in DoSomeMath()");

throw new ArithmeticException();

}

}

static void Main(string[] args)

{

try

{

DoSomeMath();

}

catch (ArithmeticException e)

{

Console.WriteLine("Hmm, there was an error in there, be careful!");

}

Console.ReadLine();

}

}14

15

13.3 Example: Handling DivideByZeroExceptions and FormatExceptions

16

17

18

19

20

13.4 .NET Exception Hierarchy

21

13.5 finally Block

22

23

24

25

26

27

28

29

30

31

13.7 Exception Properties

32

33

34

35

36

37

13.8 User-Defined Exception Classes

38

39

40

41

42

43