복습 - 오류 처리
Coddy C# 여정의 논리 및 흐름 섹션에 포함된 레슨. 66개 중 30번째.
챌린지
중급processUserData라는 메서드를 다음과 같이 만드세요:
- 두 개의 매개변수를 받습니다: string
filePath및 intuserAge - using 문을 사용하여
filePath의 파일에서 첫 번째 줄을 읽습니다 userAge가 18 이상 120 이하인지 확인합니다- Age가 유효하지 않으면 "Age must be between 18 and 120" 메시지와 함께
InvalidUserAgeException이라는 custom exception을 throw합니다 - file이 존재하지 않으면 FileNotFoundException을 catch하고 "User file not found"를 출력합니다
- 다른 IO exception이 occur하면 이를 catch하고 "Error reading user data: [exception message]"를 출력합니다
- 그 밖의 모든 exception에 대해서는 "An unexpected error occurred"를 출력합니다
- 성공하면 file content를 return하고, exception이 occur하면 "No data"를 return합니다
직접 해보기
using System;
using System.IO;
class ProcessUserData
{
// 여기에 사용자 정의 예외를 만드세요
public static string processUserData(string filePath, int userAge)
{
// 여기에 코드를 작성하세요
}
}논리 및 흐름의 모든 레슨
직접 연습해 보세요: 온라인 C# 컴파일러