Recap - Error Handling
Part of the Logic & Flow section of Coddy's C# journey — lesson 30 of 66.
Challenge
MediumCreate a method called processUserData that:
- Takes two parameters: a string
filePathand an intuserAge - Uses a using statement to read the first line from the file at
filePath - Verifies that
userAgeis between 18 and 120 (inclusive) - If the age is invalid, throw a custom exception called
InvalidUserAgeExceptionwith the message "Age must be between 18 and 120" - If the file doesn't exist, catch the FileNotFoundException and print "User file not found"
- If another IO exception occurs, catch it and print "Error reading user data: [exception message]"
- For any other exceptions, print "An unexpected error occurred"
- 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
1Multi-dimensional Arrays
2D Arrays BasicsDeclaring and Initializing 2DAccessing 2D Array ElementsNested Loops with 2D ArraysJagged ArraysCommon Matrix OperationsRecap - Multi-dimensional4Flow Control Techniques
Early ReturnsGuard ClausesJump Statements (goto)Break and ContinueFlatten Nested Conditionals7Logical Operators Advanced
Short-Circuit EvaluationConditional Logical OperatorsOperator PrecedenceRecap - Advanced Operators2Advanced Decision Making
Multiple ConditionsComplex Boolean LogicIf vs. Switch ComparisonNested Switch StatementsRecap - Advanced Decisions5Exception Handling
Try-Catch BasicsException TypesMultiple Catch BlocksWorking with FilesFinally BlockUsing vs. Try-FinallyCustom ExceptionsRecap - Error Handling3Loop Enhancements
Loop PerformanceIterating ComplexEach Loop TypeRefactoring LoopsRecap - Optimized Loops6Null Handling
Null Reference BasicsNullable Value TypesNull Checking PatternsDefensive ProgrammingRecap - Null Safety