Back
Close

C# Professional - Basics & OOP - Exercises

talent-agile
7,545 views
Previous: Exception Handling Next: Equality - Exercises

Exception Handling - Mini-Project

This mini project will help you understand how to handle exceptions from real code.

Logging exceptions

The basic need is the simple one.

Change the code to make sure that all exceptions are logged, then rethrown to the calling code.

Catching a generic exception
·// {...}
private readonly ILog _logger;
public bool SaveListOfUsers(string filePath)
{
var allUsers = _userRepository.GetAllUsers();
var exportData = string.Join(Environment.NewLine, allUsers.Select(user => $"{user.FirstName} {user.LastName}"));
if (_fileSystem.FileExists(filePath))
{
_fileSystem.Delete(filePath);
}
_fileSystem.WriteAllLines(filePath, exportData);
return true;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Handling file I/O exceptions

As the code is writing to a file for the export, it may fail if the user running the code has insufficient rights.

We need to handle this case by adding two rules:

  • Catch exceptions of type UnauthorizedAccessException
  • Log these exceptions
  • Make sure the method will not throw an exception, but should return false to indicate that the export was not done
Handling exceptions
·// {...}
private readonly ILog _logger;
public bool SaveListOfUsers(string filePath)
{
var allUsers = _userRepository.GetAllUsers();
var exportData = string.Join(Environment.NewLine, allUsers.Select(user => $"{user.FirstName} {user.LastName}"));
if (_fileSystem.FileExists(filePath))
{
_fileSystem.Delete(filePath);
}
_fileSystem.WriteAllLines(filePath, exportData);
return true;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Wrap other exceptions

If a NullReferenceException is raised, mainly for technical issues, we will catch it and wrap it in an InvalidOperationException with a proper exception message indicating that the export was not done.

Implement these new rules:

  • Catch exceptions of type NullReferenceException
  • Log the exception
  • Make the method throw a new exception of type InvalidOperationException
    • The new exception must have a message "Could not create the report, check logs for more details"
    • The new exception must have a reference to the caught one
Wrapping in a new exception
·// {...}
private readonly ILog _logger;
public bool SaveListOfUsers(string filePath)
{
var allUsers = _userRepository.GetAllUsers();
var exportData = string.Join(Environment.NewLine, allUsers.Select(user => $"{user.FirstName} {user.LastName}"));
if (_fileSystem.FileExists(filePath))
{
_fileSystem.Delete(filePath);
}
_fileSystem.WriteAllLines(filePath, exportData);
return true;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

All combined

Before pushing our code to production, make sure that all three cases are handled correctly at the same time.

Wrapping in a new exception
·// {...}
private readonly ILog _logger;
public bool SaveListOfUsers(string filePath)
{
var allUsers = _userRepository.GetAllUsers();
var exportData = string.Join(Environment.NewLine, allUsers.Select(user => $"{user.FirstName} {user.LastName}"));
if (_fileSystem.FileExists(filePath))
{
_fileSystem.Delete(filePath);
}
_fileSystem.WriteAllLines(filePath, exportData);
return true;
}
// {...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants