Exercise: Industrial Reactor Temperature Monitor

Validate IoT sensor data streams and build multiple custom exception types to handle hardware faults and critical overheating.

Problem statement

You are developing monitoring software for an industrial reactor facility. The system reads temperatures in Celsius from various IoT sensors. Faulty sensors might report physically impossible temperatures below absolute zero (-273.15°C). Additionally, if the reactor temperature exceeds 1000.0°C, it indicates a critical thermal breach. Your utility must evaluate a batch of sensor readings, flag any threshold breaches by throwing the appropriate custom exception, and continue processing the rest of the batch.

Task requirements

  • Create two custom exception classes: InvalidTemperatureException and CriticalOverheatingException.

  • Include a property in both exceptions to store the exact faulty temperature reading.

  • Write a method named ValidateReading that takes a single double temperature.

  • If the reading is above 1000.0°C, throw the CriticalOverheatingException.

  • If the reading is below -273.15°C, throw the InvalidTemperatureException.

  • In the top-level entry point, use a foreach loop to iterate through the array of readings.

  • Place a try...catch...finally block inside the loop to intercept these specific exceptions.

  • Print a distinct alert for each exception type, and ensure a “Sensor check complete.” message logs for every sensor.

Constraints

  • Inherit both custom exceptions from the standard ArgumentException class.

  • Use the throw keyword to manually trigger your custom exceptions.

  • Use multiple specific catch blocks ordered sequentially to target your custom exception types.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • By putting the try...catch inside the foreach loop, an exception will only skip the rest of the current iteration. This allows the loop to continue to the next temperature reading.

  • Both of your custom exception classes will look structurally identical but have different class names to represent the distinct types of failures.

  • Make sure your constructors accept both a string message and a double faultyReading. Pass the message parameter to the parent constructor using : base(message).

Exercise: Industrial Reactor Temperature Monitor

Validate IoT sensor data streams and build multiple custom exception types to handle hardware faults and critical overheating.

Problem statement

You are developing monitoring software for an industrial reactor facility. The system reads temperatures in Celsius from various IoT sensors. Faulty sensors might report physically impossible temperatures below absolute zero (-273.15°C). Additionally, if the reactor temperature exceeds 1000.0°C, it indicates a critical thermal breach. Your utility must evaluate a batch of sensor readings, flag any threshold breaches by throwing the appropriate custom exception, and continue processing the rest of the batch.

Task requirements

  • Create two custom exception classes: InvalidTemperatureException and CriticalOverheatingException.

  • Include a property in both exceptions to store the exact faulty temperature reading.

  • Write a method named ValidateReading that takes a single double temperature.

  • If the reading is above 1000.0°C, throw the CriticalOverheatingException.

  • If the reading is below -273.15°C, throw the InvalidTemperatureException.

  • In the top-level entry point, use a foreach loop to iterate through the array of readings.

  • Place a try...catch...finally block inside the loop to intercept these specific exceptions.

  • Print a distinct alert for each exception type, and ensure a “Sensor check complete.” message logs for every sensor.

Constraints

  • Inherit both custom exceptions from the standard ArgumentException class.

  • Use the throw keyword to manually trigger your custom exceptions.

  • Use multiple specific catch blocks ordered sequentially to target your custom exception types.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • By putting the try...catch inside the foreach loop, an exception will only skip the rest of the current iteration. This allows the loop to continue to the next temperature reading.

  • Both of your custom exception classes will look structurally identical but have different class names to represent the distinct types of failures.

  • Make sure your constructors accept both a string message and a double faultyReading. Pass the message parameter to the parent constructor using : base(message).

C# 14.0
double[] sensorReadings = { 50.5, 120.0, 1050.0, -300.0, 10.0 };
foreach (double reading in sensorReadings)
{
// TODO: Inside this loop, create a try-catch-finally block.
// In the try block, call ValidateReading(reading).
// Add a catch block for CriticalOverheatingException and print a thermal breach alert.
// Add a catch block for InvalidTemperatureException and print a sensor fault alert.
// Use finally to print "Sensor check complete."
}
// Helper Method
void ValidateReading(double temp)
{
// TODO: Check if temp is greater than 1000.0. If so, throw CriticalOverheatingException.
// TODO: Check if temp is less than -273.15. If so, throw InvalidTemperatureException.
Console.WriteLine($"Reading valid: {temp}°C");
}
// Custom Exception Definitions
// TODO: Define InvalidTemperatureException inheriting from ArgumentException.
// Add a public double property named FaultyReading (with a getter).
// Create a constructor to set the message and the faulty reading.
// TODO: Define CriticalOverheatingException inheriting from ArgumentException.
// Add a public double property named FaultyReading (with a getter).
// Create a constructor to set the message and the faulty reading.