簡體   English   中英

嘗試/捕獲崩潰255

[英]Try/catch crash 255

是try / catch的新手,需要在程序中使用它。 由於某種原因,盡管我的程序在捕獲obj時崩潰了。 我編寫的示例程序只是讀取一個數字,然后進行測試以查看其是否小於10或大於20。關鍵是要使用兩個用戶定義的類進行拋出和捕獲。 上面的20個obj被拋出並捕獲就好了。 但是如果低於10,則將其拋出崩潰。 為什么是這樣? 這是我的代碼

#include <iostream>
#include <cstdlib>

using namespace std;

class above20
{
    public:
        above20()
        {
            msg = "Number is above 20.";
        };
        string getmsg()
        {
            cout<<msg<<endl;
        };

    private:
        string msg;
};


class below10
{
    public:
        below10()
        {
            msg = "Number is below 10";
        };
        string getmsg()
        {
            cout<<msg<<endl;
        };

    private:
        string msg;
};

int main ()
{
    int num;
    try
    {
        cout<<"Enter Number between 10 and 20:"<<endl;
        cin >> num;

        if (num > 20)
        {
            throw above20 ();
        }
    }
    catch (above20 obj)
    {
        obj.getmsg();
    }

    try
    {
        if (num < 10)
        {
            throw below10 ();
        }   
    }
    catch (below10 obj)
    {
        obj.getmsg();
    }

    system ("pause");
    return (0);
}

您確定會編譯嗎? 您是否在復制粘貼中遺漏了某些內容? getmsg()方法不返回任何內容。

---編輯---試試這個:

 void getmsg()
 {
        cout<<msg<<endl;
 };

您的代碼中語法很多。 您得到的錯誤是因為您用返回值聲明getMsg而不返回(UB-您很幸運它甚至可以編譯!)。

要解決所有問題,請訪問: http : //ideone.com/1qGAuR

您的代碼中有一些錯誤,例如,盡管說應該在簽名中包含一個不返回任何內容的函數:

    string getmsg()
    {
        cout<<msg<<endl;
    };

確實應該是:

    void getmsg()
    {
        cout<<msg<<endl;
    };

要么

    string getmsg()
    {
        return string(msg);
    };

從設計的角度來看,將這些錯誤擱置一秒鍾,從一個異常基類繼承是更干凈的。 我通常會繼承自std::runtime_error但如果需要,您可以定義自己的。 例如 :

#include <iostream>
#include <cstdlib>

using namespace std;

class above_below_exception
{ 
    public:
        virtual string getmsg() =0;
};

class above20 : public above_below_exception
{
    public:
        above20()
        {
            msg = "Number is above 20.";
        };
        string getmsg()
        {
            return string(msg);
        };
    private:
        string msg;
};


class below10 : public above_below_exception
{
    public:
        below10()
        {
            msg = "Number is below 10";
        };
        string getmsg()
        {
            return string(msg);
        };
    private:
        string msg;

};

int main ()
{
    int num;
    try
    {
        cout<<"Enter Number between 10 and 20:"<<endl;
        cin >> num;

        if (num > 20)
        {
            throw above20();
        }
        if (num < 10)
        {
            throw below10();
        }
    }
    catch (above_below_exception& obj)
    {
        cout << obj.getmsg();
    }

return (0);
}

您的代碼的潛在解決方案:

#include <iostream>
#include <cstdlib>

using namespace std;

class above20 : public std::exception
{
    public:
        above20()
        {
            msg = "Number is above 20.";
        }

        virtual ~above20 () throw ()
        {

        }

        string getmsg ()
        {
            cout << msg << endl;
            return msg;
        }

    private:
        string msg;

};


class below10 : public std::exception
{
    public:
        below10()
        {
            msg = "Number is below 10";
        }

        virtual ~below10 () throw ()
        {

        }

        string getmsg()
        {
            cout << msg << endl;
            return msg;
        }

    private:
        string msg;

};

int main ()
{
    int num;
    try
    {

        cout<<"Enter Number between 10 and 20:"<<endl;
        cin >> num;

        if (num > 20)
        {
            throw above20 ();
        }
    }
    catch (above20 &obj)
    {
        cout << obj. getmsg () << endl;
    }

    try
    {
        if (num < 10)
        {
            throw below10 ();
        }
    }
    catch (below10 obj)
    {
        obj.getmsg();
    }


system ("pause");
return (0);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM