Menu
Coddy logo textTech

복습 - 오류 처리

Coddy C# 여정의 논리 및 흐름 섹션에 포함된 레슨. 66개 중 30번째.

challenge icon

챌린지

중급

processUserData라는 메서드를 다음과 같이 만드세요:

  1. 두 개의 매개변수를 받습니다: string filePath 및 int userAge
  2. using 문을 사용하여 filePath의 파일에서 첫 번째 줄을 읽습니다
  3. userAge가 18 이상 120 이하인지 확인합니다
  4. Age가 유효하지 않으면 "Age must be between 18 and 120" 메시지와 함께 InvalidUserAgeException이라는 custom exception을 throw합니다
  5. file이 존재하지 않으면 FileNotFoundException을 catch하고 "User file not found"를 출력합니다
  6. 다른 IO exception이 occur하면 이를 catch하고 "Error reading user data: [exception message]"를 출력합니다
  7. 그 밖의 모든 exception에 대해서는 "An unexpected error occurred"를 출력합니다
  8. 성공하면 file content를 return하고, exception이 occur하면 "No data"를 return합니다

직접 해보기

using System;
using System.IO;

class ProcessUserData
{
    // 여기에 사용자 정의 예외를 만드세요
    
    public static string processUserData(string filePath, int userAge)
    {
        // 여기에 코드를 작성하세요
    }
}

논리 및 흐름의 모든 레슨

직접 연습해 보세요: 온라인 C# 컴파일러