You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

代码未处理异常:C++自定义异常类与房产类代码技术问询

为你的C++房产类实现完善的异常处理方案

首先,先把你提供的未完成代码整理出来,方便咱们分析:

#include<iostream> 
#include<string> 
using namespace std; 

class nonIntegerException : public runtime_error { 
private: 
    string nonIntegerData; 
public: 
    nonIntegerException(string); 
    string getNonIntegerData(); 
}; 

class RealEstate { 
    friend ostream& operator<<(ostream&, const RealEstate); 
    friend istream& operator>>(istream&, RealEstate&); 
private: 
    int housePrice; 
    int numBeds; 
    int numBaths; 
}; 

// 原代码中这里的runtime_error构造参数未完成
nonIntegerException::nonIntegerException(string s) : runtime_error(...)

接下来咱们一步步补全并添加异常处理逻辑:

1. 完善自定义异常类nonIntegerException

因为它继承自标准库的runtime_error,构造函数必须传递一个错误信息字符串给基类;同时要实现getNonIntegerData方法来返回导致异常的非法输入内容:

nonIntegerException::nonIntegerException(string s) 
    : runtime_error("Non-integer input detected: " + s), nonIntegerData(s) {}

string nonIntegerException::getNonIntegerData() {
    return nonIntegerData;
}

这里咱们把非法输入内容拼接到基类的错误信息里,同时存在自己的私有成员中,方便后续捕获异常时获取具体的错误数据。

2. 实现RealEstate的输入输出运算符,添加异常抛出逻辑

重点是在operator>>里处理输入验证:当尝试读取整数失败时,我们需要捕获输入流的错误状态,提取非法输入内容,然后抛出nonIntegerException:

ostream& operator<<(ostream& os, const RealEstate& re) {
    os << "House Price: " << re.housePrice << ", Beds: " << re.numBeds << ", Baths: " << re.numBaths;
    return os;
}

istream& operator>>(istream& is, RealEstate& re) {
    // 临时变量存储输入,方便验证
    string priceInput, bedsInput, bathsInput;
    
    // 先按字符串读取,避免输入非整数直接导致流状态失败
    is >> priceInput >> bedsInput >> bathsInput;
    
    // 尝试将字符串转换为整数,转换失败则抛出异常
    try {
        re.housePrice = stoi(priceInput);
        re.numBeds = stoi(bedsInput);
        re.numBaths = stoi(bathsInput);
    } catch (const invalid_argument& e) {
        // 找出第一个转换失败的输入
        string invalidData;
        if (stoi(priceInput) == 0 && errno == EINVAL) invalidData = priceInput;
        else if (stoi(bedsInput) == 0 && errno == EINVAL) invalidData = bedsInput;
        else invalidData = bathsInput;
        
        throw nonIntegerException(invalidData);
    } catch (const out_of_range& e) {
        // 处理整数超出范围的情况
        throw nonIntegerException("Input value out of integer range");
    }
    
    return is;
}

这里咱们没有直接用is >> re.housePrice,而是先按字符串读取,再用stoi转换——这样能更精准地捕获非整数输入的情况,还能处理数值超出整数范围的问题。

3. 示例主函数:捕获并处理异常

最后写一个主函数来演示如何使用这个带异常处理的类:

int main() {
    RealEstate myHouse;
    cout << "Enter house price, number of beds, number of baths: ";
    
    try {
        cin >> myHouse;
        cout << "\nYour real estate details:\n" << myHouse << endl;
    } catch (const nonIntegerException& e) {
        cerr << "\nError: " << e.what() << endl;
        cerr << "Invalid input data: " << e.getNonIntegerData() << endl;
    } catch (const exception& e) {
        // 捕获其他标准异常
        cerr << "\nUnexpected error: " << e.what() << endl;
    }
    
    return 0;
}

这样,当用户输入类似300000 three 2这样的内容时,程序会抛出nonIntegerException,并输出具体的错误信息和非法输入的内容,而不是直接崩溃或者进入错误的流状态。

额外提示

  • 尽量避免使用using namespace std;,在大型项目中容易引发命名冲突,建议改用std::前缀
  • 如果你需要更细粒度的异常,可以拆分出invalidPriceException、invalidBedCountException等,但对于简单场景,一个nonIntegerException足够覆盖非整数输入的问题

内容的提问来源于stack exchange,提问作者Preston

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:23:15