Core Programmingนายสมเกียรติ สอนนวลCimatt Business Group Co.,LTD.
Core ProgrammingUnderstand computerstorage and data typesUnderstand computerdecision structuresIdentify the appropriatemethod for handlingrepetitionUnderstand errorhandling
Understand errorhandlingLesson OverviewStudents will understand error handling. In this lesson, you will learn:Structured exception handling using the try, catch, finally, and throw keywords
Understand errorhandlingReview TermExceptionA problem or change in conditions that causes the microprocessor to stop what it is doing and handle the situation in a separate routine.
Understand errorhandlingHow to Handle ExceptionsA 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.
Understand errorhandlingExample 1: try and catchintSafeDivision(int x, int y){try{	return (x / y);}catch (DivideByZeroExceptiondbz){Console.WriteLine("Division by zero attempted!");return 0;}}
Understand errorhandlingExample 2: try, catch, and finallystatic void Main(){try{	//statement which can cause exception}catch (Exception e){	//statement for handling exception}finally{	//cleanup code}}
Understand errorhandlingExample 3: Throwing an Exceptionstatic void CopyObject(SampleClass original){if (original == null){throw new ArgumentException("Parameter cannot be null", "original");}//Some code}
AssignmentProvide 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());}}
Answer#1) DivideByZeroExceptionint x = 100/0;#2) IndexOutOfRangeExceptionint[] array = new int[3];		array[4] = 31;#3) NullReferenceException		Object x;x.toString();#4) StackOverflowException		method4();

1.4 core programming [understand error handling]

  • 1.
  • 2.
    Core ProgrammingUnderstand computerstorageand data typesUnderstand computerdecision structuresIdentify the appropriatemethod for handlingrepetitionUnderstand errorhandling
  • 3.
    Understand errorhandlingLesson OverviewStudentswill understand error handling. In this lesson, you will learn:Structured exception handling using the try, catch, finally, and throw keywords
  • 4.
    Understand errorhandlingReview TermExceptionAproblem or change in conditions that causes the microprocessor to stop what it is doing and handle the situation in a separate routine.
  • 5.
    Understand errorhandlingHow toHandle ExceptionsA 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 errorhandlingExample 1:try and catchintSafeDivision(int x, int y){try{ return (x / y);}catch (DivideByZeroExceptiondbz){Console.WriteLine("Division by zero attempted!");return 0;}}
  • 7.
    Understand errorhandlingExample 2:try, catch, and finallystatic void Main(){try{ //statement which can cause exception}catch (Exception e){ //statement for handling exception}finally{ //cleanup code}}
  • 8.
    Understand errorhandlingExample 3:Throwing an Exceptionstatic void CopyObject(SampleClass original){if (original == null){throw new ArgumentException("Parameter cannot be null", "original");}//Some code}
  • 9.
    AssignmentProvide the codein 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) DivideByZeroExceptionint x= 100/0;#2) IndexOutOfRangeExceptionint[] array = new int[3]; array[4] = 31;#3) NullReferenceException Object x;x.toString();#4) StackOverflowException method4();