C++自定义ZeroCheck异常输出格式不符合预期的问题求助
Let's break down what's going wrong here and fix it step by step!
The Root Cause
Your issue stems from how the operator<< is structured: when you output Math3, the operator first prints "Quotient is: " to the stream, then calls division()—which throws the ZeroCheck exception. By the time the exception is caught, the "Quotient is: " text is already in the output and can't be undone, leading to the messy combined message. Additionally, your ZeroCheck class's what() method doesn't follow C++ exception best practices (it's not a const member function), which can cause subtle issues.
Fixes to Implement
1. Update the ZeroCheck Exception Class
Align it with standard C++ exception conventions by making what() a const member function, and use initializer lists for cleaner construction:
// ZeroCheck.h #ifndef ZEROCHECK_H_INCLUDED #define ZEROCHECK_H_INCLUDED #include <string> using std::string; class ZeroCheck { public: ZeroCheck() : msg("") {} ZeroCheck(const string& _msg) : msg(_msg) {} // Make what() const and return a const reference for efficiency const string& what() const { return msg; } private: string msg; }; #endif /* ZEROCHECK_H_INCLUDED */
2. Rewrite the operator<< to Handle Exceptions Before Outputting the Quotient Label
We'll adjust the logic to first attempt calculating the quotient, then decide what to print—this way, we never output "Quotient is: " if division fails:
// In main.cpp, replace your existing operator<< with this template<class T> ostream& operator<<(ostream& os, Mathematics<T>& obj) { os << "The result of calculation for: " << obj.val1 << " and " << obj.val2 << '\n' << "Sum is: " << obj.addition() << '\n' << "Difference is: " << obj.subtraction() << '\n' << "Product is: " << obj.multiplication() << '\n'; // Handle division separately to avoid premature label output try { T quotient = obj.division(); os << "Quotient is: " << quotient << '\n'; } catch (const ZeroCheck& e) { // Print only the error message when division fails os << e.what() << '\n'; } return os; }
3. Fix the Minor Typo in main()
Your expected output has Math 3: (with a colon), but your code prints Math 3 without it. Correct that line:
// In main.cpp cout << "Math 3:" << '\n'; // Add the missing colon
Optional Alternative: Handle Exceptions in main() Instead
If you prefer to keep exception handling centralized in main() instead of the output operator, you can split the output of Math3 into separate parts (you'll need to make val1/val2 public or add getter methods to Mathematics):
int main() { try { Mathematics<int> Math1(10, 5); Mathematics<double> Math2(5.5, 3.4); Mathematics<int> Math3(6, 0); cout << "Math 1:" << '\n'; cout << Math1 << '\n' << '\n'; cout << "Math 2:" << '\n'; cout << Math2 << '\n'; cout << "Math 3:" << '\n'; // Print non-division results first cout << "The result of calculation for: " << Math3.val1 << " and " << Math3.val2 << '\n' << "Sum is: " << Math3.addition() << '\n' << "Difference is: " << Math3.subtraction() << '\n' << "Product is: " << Math3.multiplication() << '\n'; // Attempt division separately so exceptions are caught in main() int quotient = Math3.division(); cout << "Quotient is: " << quotient << '\n'; } catch (const ZeroCheck& e) { cerr << e.what() << '\n'; } return EXIT_SUCCESS; }
Final Result
After applying these changes, your output will match exactly what you expected:
Math 1:
The result of calculation for: 10 and 5
Sum is: 15
Difference is: 5
Product is: 50
Quotient is: 2Math 2:
The result of calculation for: 5.5 and 3.4
Sum is: 8.9
Difference is: 2.1
Product is: 18.7
Quotient is: 1.61765Math 3:
The result of calculation for: 6 and 0
Sum is: 6
Difference is: 6
Product is: 0
Denominator is 0, invalid division.
内容的提问来源于stack exchange,提问作者jr.çhåvez__07

