public class TemperatureSensor{ private double prim_read(){ if (Math.random()<0.8) return -300; else return Math.random()*70-20; // -20..50 } public double readTemperature() throws SensorError{ double t = prim_read(); if (t == -300) throw new SensorError(); else return t; } class SensorError extends Exception { } public static void monitorTemperature(double low, double high){ TemperatureSensor ts = new TemperatureSensor(); int errorReadings = 0; for (int i=0;i<300;i++){ try{ double t = ts.readTemperature(); errorReadings = 0; if (thigh) System.out.println("High temperature: " + t); }catch(SensorError re){ errorReadings++; if (errorReadings>5){ System.out.println("Cannot read the temperature"); errorReadings = 0; } } try{ Thread.sleep(1);}catch(InterruptedException ie){}; } } public static void tryMe(){ monitorTemperature(-15,45); System.out.println("------------------"); } }