Menu
Coddy logo textTech

Recap - Error Handling

Part of the Logic & Flow section of Coddy's C# journey — lesson 30 of 66.

challenge icon

Challenge

Medium

Create a method called processUserData that:

  1. Takes two parameters: a string filePath and an int userAge
  2. Uses a using statement to read the first line from the file at filePath
  3. Verifies that userAge is between 18 and 120 (inclusive)
  4. If the age is invalid, throw a custom exception called InvalidUserAgeException with the message "Age must be between 18 and 120"
  5. If the file doesn't exist, catch the FileNotFoundException and print "User file not found"
  6. If another IO exception occurs, catch it and print "Error reading user data: [exception message]"
  7. For any other exceptions, print "An unexpected error occurred"
  8. Return the file content if successful or "No data" if any exception occurs

Try it yourself

using System;
using System.IO;

class ProcessUserData
{
    // Create your custom exception here
    
    public static string processUserData(string filePath, int userAge)
    {
        // Write your code here
    }
}

All lessons in Logic & Flow