Menu
Coddy logo textTech

復習 - エラー処理

CoddyのC#ジャーニー「ロジックとフロー」セクションの一部。レッスン 30/66。

challenge icon

チャレンジ

中級

processUserDataという名前のメソッドを作成してください。このメソッドは次の処理を行います:

  1. 2つのパラメーターを受け取ります:string filePathとint userAge
  2. usingステートメントを使用して、filePathにあるfileの最初の行を読み取ります
  3. userAgeが18から120の間(両端を含む)であることを確認します
  4. Ageが無効な場合、"Age must be between 18 and 120"というmessageを持つ、InvalidUserAgeExceptionという名前のcustom exceptionをthrowします
  5. fileが存在しない場合は、FileNotFoundExceptionをcatchして"User file not found"と出力します
  6. 別のIO exceptionがoccurredした場合は、それをcatchして"Error reading user data: [exception message]"と出力します
  7. その他のexceptionについては、"An unexpected error occurred"と出力します
  8. 成功した場合はfileのcontentをreturnし、いずれかのexceptionがoccurredした場合は"No data"をreturnします

自分で試してみよう

using System;
using System.IO;

class ProcessUserData
{
    // ここにカスタム例外を作成してください
    
    public static string processUserData(string filePath, int userAge)
    {
        // ここにコードを書いてください
    }
}

ロジックとフローのすべてのレッスン

自分で練習してみよう: C#オンラインコンパイラ