如何正确为Resistor类搭建try-catch异常捕获块?
Hey there! Let's break down why your try-catch block isn't working and get your Resistor class tests up and running properly.
First: Fix the Logic Bug in Your Resistor Constructor
Looking at your Resistor constructor, there's a critical logic error in the power rating check:
if (powerrating >=0){ throw new Exception("The power rating must be greater than zero."); }
This throws an exception when the power rating is greater than or equal to 0—the exact opposite of what you want! You should throw the exception only when powerrating <= 0, since your requirement states the power rating must be greater than zero.
Second: Your Test Code Isn't Triggering Exceptions
The biggest issue with your test is that you're not actually creating a Resistor instance inside the try block. You just define a resistancevalue variable, but never call the Resistor constructor—so none of the exception checks in the constructor run at all. That's why your catch block never executes.
Fixed Code Examples
Corrected Resistor Constructor
public Resistor(double resistancevalue, double tolerancevalue, double powerrating) throws Exception { if (resistancevalue <= 0) { throw new Exception("The resistance value must be greater than zero."); } if (tolerancevalue <= 0 || tolerancevalue >= 1) { throw new Exception("The tolerance value must be between zero and one (not inclusive)."); } // Fixed power rating check if (powerrating <= 0) { throw new Exception("The power rating must be greater than zero."); } else { this.resistancevalue = resistancevalue; this.tolerancevalue = tolerancevalue; this.powerrating = powerrating; } } // Keep your existing methods as-is public double getResistanceValue(){ return resistancevalue; } double miniResistance(){ double Minir = resistancevalue*(1.0-tolerancevalue); return Minir; } double maxResistance(){ double Maxr= resistancevalue*(1.0+tolerancevalue); return Maxr; }
Working Test Class
Let's write tests that cover all possible failure scenarios plus a valid case:
public class ResistorTest { public static void main(String[] args) { double validTolerance = 0.5; double validPower = 1.0; // Test 1: Invalid resistance value (<=0) try { Resistor badResistance = new Resistor(-1, validTolerance, validPower); System.out.println("Test 1 Failed: Exception not thrown for invalid resistance"); } catch (Exception e) { System.out.println("Test 1 Passed: " + e.getMessage()); } // Test 2: Invalid tolerance value (<=0) try { Resistor badToleranceLow = new Resistor(10, -0.1, validPower); System.out.println("Test 2 Failed: Exception not thrown for low tolerance"); } catch (Exception e) { System.out.println("Test 2 Passed: " + e.getMessage()); } // Test 3: Invalid tolerance value (>=1) try { Resistor badToleranceHigh = new Resistor(10, 1.0, validPower); System.out.println("Test 3 Failed: Exception not thrown for high tolerance"); } catch (Exception e) { System.out.println("Test 3 Passed: " + e.getMessage()); } // Test 4: Invalid power rating (<=0) try { Resistor badPower = new Resistor(10, validTolerance, 0); System.out.println("Test 4 Failed: Exception not thrown for invalid power"); } catch (Exception e) { System.out.println("Test 4 Passed: " + e.getMessage()); } // Test 5: Valid Resistor instance try { Resistor goodResistor = new Resistor(100, 0.05, 2.0); System.out.println("\nTest 5 Passed: Valid resistor created"); System.out.println("Base Resistance: " + goodResistor.getResistanceValue()); System.out.println("Min Resistance: " + goodResistor.miniResistance()); System.out.println("Max Resistance: " + goodResistor.maxResistance()); } catch (Exception e) { System.out.println("Test 5 Failed: " + e.getMessage()); } } }
Key Takeaways
- Always instantiate the class you're testing inside the try block to trigger constructor-level exceptions.
- Double-check conditional logic in the constructor to ensure exceptions are thrown for the right cases (like the power rating fix we did).
- Test all possible failure scenarios plus a valid case to confirm your class behaves as expected.
内容的提问来源于stack exchange,提问作者TwilightLuigi

