연습 - 특정 예외를 catch하기 위한 챌린지 작업 완료
이 모듈의 코드 챌린지는 학습한 내용을 강화하고 계속하기 전에 자신감을 얻는 데 사용됩니다.
특정 예외 문제 catch
이 챌린지에서는 여러 다른 예외 형식을 생성하는 코드 샘플이 제공됩니다. 단일 try 코드 블록에는 예외를 생성하는 코드가 포함됩니다. 특정 예외 유형을 처리하기 위해 여러 catch 절이 포함됩니다.
각 예외가 catch되고 해당 오류 메시지가 콘솔에 표시되도록 코드 샘플을 업데이트해야 합니다.
이 과제에 대한 요구 사항은 다음과 같습니다.
Program.cs 파일에 다음 코드 샘플이 포함되어 있는지 확인합니다.
try { int num1 = int.MaxValue; int num2 = int.MaxValue; int result = num1 + num2; Console.WriteLine("Result: " + result); string str = null; int length = str.Length; Console.WriteLine("String Length: " + length); int[] numbers = new int[5]; numbers[5] = 10; Console.WriteLine("Number at index 5: " + numbers[5]); int num3 = 10; int num4 = 0; int result2 = num3 / num4; Console.WriteLine("Result: " + result2); } catch (OverflowException ex) { Console.WriteLine("Error: The number is too large to be represented as an integer." + ex.Message); } catch (NullReferenceException ex) { Console.WriteLine("Error: The reference is null." + ex.Message); } catch (IndexOutOfRangeException ex) { Console.WriteLine("Error: Index out of range." + ex.Message); } catch (DivideByZeroException ex) { Console.WriteLine("Error: Cannot divide by zero." + ex.Message); } Console.WriteLine("Exiting program.");해당 예외 유형이 발생할 때 각 오류 메시지가 콘솔에 표시되도록 코드를 업데이트합니다.
업데이트된 코드가 콘솔에 다음 메시지를 출력하는지 확인합니다.
Error: The number is too large to be represented as an integer. Arithmetic operation resulted in an overflow. Error: The reference is null. Object reference not set to an instance of an object. Error: Index out of range. Index was outside the bounds of the array. Error: Cannot divide by zero. Attempted to divide by zero. Exiting program.
행운을 빌어!