Solution: Industrial Reactor Temperature Monitor

Review the implementation of multiple custom exceptions and targeted error handling using chained catch blocks.

Solution: Industrial Reactor Temperature Monitor

Review the implementation of multiple custom exceptions and targeted error handling using chained catch blocks.
C# 14.0
double[] sensorReadings = { 50.5, 120.0, 1050.0, -300.0, 10.0 };
foreach (double reading in sensorReadings)
{
try
{
ValidateReading(reading);
}
catch (CriticalOverheatingException ex)
{
Console.WriteLine($"MELTDOWN ALERT: {ex.Message}");
Console.WriteLine($"Peak Temperature: {ex.FaultyReading}°C");
}
catch (InvalidTemperatureException ex)
{
Console.WriteLine($"SENSOR FAULT: {ex.Message}");
Console.WriteLine($"Faulty Value: {ex.FaultyReading}°C");
}
finally
{
Console.WriteLine("--- Sensor check complete ---");
}
}
// --- Helper Method ---
void ValidateReading(double temp)
{
if (temp > 1000.0)
{
throw new CriticalOverheatingException("Reactor exceeded safe thermal limits!", temp);
}
if (temp < -273.15)
{
throw new InvalidTemperatureException("Physically impossible temperature detected!", temp);
}
Console.WriteLine($"Reading valid: {temp}°C");
}
// --- Custom Exception Definitions ---
public class CriticalOverheatingException : ArgumentException
{
public double FaultyReading { get; }
public CriticalOverheatingException(string message, double faultyReading)
: base(message)
{
FaultyReading = faultyReading;
}
}
public class InvalidTemperatureException : ArgumentException
{
public double FaultyReading { get; }
public InvalidTemperatureException(string message, double faultyReading)
: base(message)
{
FaultyReading = faultyReading;
}
}