???? (Exception Handling) - PowerPoint PPT Presentation

About This Presentation
Title:

???? (Exception Handling)

Description:

(Exception Handling) / / – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 33
Provided by: Shyh2
Category:

less

Transcript and Presenter's Notes

Title: ???? (Exception Handling)


1
????(Exception Handling)
  • ???
  • ??????
  • ??????/???????/
  • ???????????

2
?????????(1/3)
  • readFile
  • openFile
  • determineSize
  • allocateMemory
  • readFileIntoMemory
  • closeFile
  • ????, W. D. Mitchell??, JAVA ??????, ?????,
    2001, pp. 3-8 3-10.

3
?????????(2/3)
  • errorCodeType readFile
  • initialize errorCode 0
  • openFile
  • if( fileIsOpen )
  • determineSize
  • if( gotFileLength )
  • allocateEnoughMemory
  • if( memoryAllocated )
  • readFileIntoMemory
  • if( readFailed )
  • errorCode -1 handleError
  • else
  • errorCode -2 handleError

4
?????????(3/3)
  • else
  • errorCode -3 handleError
  • closeFile
  • if( fileDidNotClose errorCode 0 )
  • errorCode -4 handleError
  • else
  • errorCode -5 handleError
  • return errorCode

5
Try-Catch ??????(1/2)
  • readFile
  • try
  • openFile
  • determineSize
  • allocateMemory
  • readFileIntoMemory
  • closeFile

6
Try-Catch ??????(2/2)
  • catch( fileDidNotOpen )
  • handleError // or throwException
  • catch( sizeNotDetermined )
  • handleError // or throwException
  • catch( memoryAllocationFailed )
  • handleError // or throwException
  • catch( couldNotReadFile )
  • handleError // or throwException
  • catch( couldNotCloseFile )
  • handleError // or throwException

7
??????
  • ????????????,???????
  • ????????????,??????,????
  • ??????????, ?????????

8
UsingTryCatch.Program (1/3)
  • using System
  • namespace UsingTryCatch
  • class Program
  • /
  • ??try...catch???
  • 5/14/2007
  • /
  • static void Main(string args)

9
UsingTryCatch.Program (2/3)
  • try
  • Console.Write("???? ")
  • int number int.Parse(Console.ReadLine())
  • int result 5 / number
  • catch (Exception e)
  • Console.WriteLine("????")
  • Console.WriteLine("???? " "\n\n"
  • e.Message "\n")
  • Console.WriteLine("???????????? "
  • "\n\n" e.StackTrace "\n")

10
UsingTryCatch.Program (3/3)
  • Console.WriteLine("??????? "
  • "\n\n" e.TargetSite "\n")
  • Console.WriteLine(
  • "??????????? "
  • "\n\n" e.Source "\n")
  • Console.WriteLine("??????? "
  • "\n\n" e.ToString() "\n")

11
UsingTryCatch2.Program (1/3)
  • using System
  • namespace UsingTryCatch2
  • / ??????
  • 5/14/2007
  • /
  • class Program
  • static void Main(string args)
  • bool success false
  • do
  • try

12
UsingTryCatch2.Program (2/3)
  • Console.Write("???? ")
  • int number int.Parse(Console.ReadLine(
    ))
  • int result 5 / number
  • Console.WriteLine("????" result)
  • success true
  • catch (FormatException e)
  • Console.WriteLine("?????")
  • Console.WriteLine(e.Message)
  • catch (DivideByZeroException e)
  • Console.WriteLine("?????")
  • Console.WriteLine(e.Message)

13
UsingTryCatch2.Program (3/3)
  • catch (Exception e)
  • Console.WriteLine("????")
  • Console.WriteLine(e.Message)
  • finally
  • if (!success) Console.WriteLine("????")
  • while (!success)
  • Console.ReadLine()

14
UserDefinedException.Program (1/6)
  • using System
  • namespace UserDefinedException
  • /
  • ????????????
  • 5/14/2007
  • /
  • class Program
  • static void Main(string args)
  • int nStudents 3
  • GradeTable table new GradeTable(nStudents)

15
??
  • ??????-2????,????????????
  • ????catch??????,?????
  • ????,???????????????catch??????????????????

16
??
  • ??FileDemo??,??????????????,?????????
  • ????catch???,???????????

17
UserDefinedException.Program (2/6)
  • try
  • table0 90
  • //table1 102
  • table1 100
  • table2 55
  • Console.WriteLine("table0 "
    table0)
  • Console.WriteLine("table1 "
    table1)
  • Console.WriteLine("table2 "
    table2)
  • //Console.WriteLine("table3 "
    table3)
  • catch (GradeOutOfRangeException e)
  • Console.WriteLine(e.ToString())

18
UserDefinedException.Program (3/6)
  • catch (IndexOutOfRangeException e)
  • Console.WriteLine(e.Message)
  • catch (Exception e)
  • Console.WriteLine(e.Message)
  • Console.ReadLine()

19
UserDefinedException.Program (4/6)
  • class GradeTable
  • private int nStudents
  • private int grades
  • public GradeTable(int nStudents)
  • this.nStudents nStudents
  • grades new intnStudents
  • public int thisint index
  • get
  • return gradesindex

20
UserDefinedException.Program (5/6)
  • set
  • if (value lt 0 value gt 100)
  • throw new GradeOutOfRangeException(value)
  • gradesindex value

21
UserDefinedException.Program (6/6)
  • public class GradeOutOfRangeException
    ApplicationException
  • private string message
  • public GradeOutOfRangeException(int grade)
  • base()
  • message "??" grade "??0?100??"
  • public override string ToString()
  • return message

22
??????
  • Exception
  • ApplicationException
  • SystemException
  • ArithmeticException
  • DivideByZeroException
  • NotFiniteNumberException
  • OverflowException
  • FormatException
  • InvalidCastException
  • IndexOutOfRangeException
  • NullReferenceException
  • StackOverflowException
  • IOException

23
??
  • ????Square,?????a???????a????????????a????,???thro
    w????????
  • ???????????
  • ??????,???????-1????,?catch??,???????

24
NestedTryAndCatch.Program(1/8)
  • using System
  • namespace NestedTryAndCatch
  • /
  • ????try...catch?????
  • 5/16/2007
  • /
  • class Program
  • static void Main(string args)
  • int nStudents 1
  • bool validNStudents false

25
NestedTryAndCatch.Program(2/8)
  • do
  • Console.Write("?????? ")
  • try
  • nStudents int.Parse(Console.ReadLine()
    )
  • if (nStudents gt 0) validNStudents
    true
  • catch (FormatException e)
  • Console.WriteLine(e.Message)
  • catch (Exception e)
  • Console.WriteLine(e.Message)
  • while (!validNStudents)

26
NestedTryAndCatch.Program(3/8)
  • try
  • GradeTable table new GradeTable(nStudent
    s)
  • for(int i0 iltnStudents i)
  • bool validGrades false
  • do
  • Console.Write(
  • "???"(i1)" ????? ")
  • try
  • tablei
  • int.Parse(Console.ReadLine())
  • Console.WriteLine("table"i" "
  • tablei)

27
NestedTryAndCatch.Program(4/8)
  • validGrades true
  • catch (GradeOutOfRangeException e)
  • Console.WriteLine(e.ToString())
  • catch (FormatException e)
  • Console.WriteLine(e.Message)
  • while (!validGrades )

28
NestedTryAndCatch.Program(5/8)
  • catch (IndexOutOfRangeException e)
  • Console.WriteLine(e.Message)
  • catch (Exception e)
  • Console.WriteLine(e.Message)

29
NestedTryAndCatch.Program(6/8)
  • class GradeTable
  • private int nStudents
  • private int grades
  • public GradeTable(int nStudents)
  • this.nStudents nStudents
  • grades new intnStudents
  • public int thisint index
  • get
  • return gradesindex

30
NestedTryAndCatch.Program(7/8)
  • set
  • if (value lt 0 value gt 100)
  • throw new GradeOutOfRangeException(value)
  • gradesindex value

31
NestedTryAndCatch.Program(8/8)
  • public class GradeOutOfRangeException
    ApplicationException
  • private string message
  • public GradeOutOfRangeException(int grade)
  • base()
  • message "??" grade "?????"
  • public override string ToString()
  • return message

32
??
  • ?NestedTryAndCatch????????????,??catch??????????
  • ????????,???????End Of File???,????????
  • ????,????End Of File???
Write a Comment
User Comments (0)
About PowerShow.com