C++ 异常处理:将异常定义为对象



我对C++非常陌生,我有一个任务是创建一个用于异常处理的测试程序。我在捕获异常时遇到问题,无论是在给定类中定义的异常还是我在 main 中定义的异常。有人可以查看我所拥有的并指出我出错的地方吗?


来自教授:

#ifndef STUDENT_H
#define STUDENT_H
#include <string>
using namespace std;
class Student
{
public:
    Student();
    Student(string thisStudentID);
    void enroll(string thisSectionID);
private:
    string studentID;
    bool timeToThrow();
    static int sneakyCount;
};
#endif
#include <string>
#include <iostream>
#include "Student.h"
#include "StudentException.h"
using namespace std;
// The constructor for this class accepts a Student ID
Student::Student(string thisStudentID)
{
    // This first statement updates a variable used to help decide when to      Throw an Exception
    sneakyCount++;
    // Any text will be accepted as the student ID in this implementation
    if (!timeToThrow())
        studentID = thisStudentID;
    else
        throw StudentException("Student " + thisStudentID + " has been      expelled from this school");
}
// This default constructor shouldn't be used, so throwing an exception     isn't so artificial, its
// the right thing to do.  We will also find out if this constructor gets     called at time that we don't expect.
Student::Student()
{
    // This first statement updates a variable used to help decide when to     Throw an Exception
    sneakyCount++;
    throw StudentException("Incorrect Call to Student Constructor - No     Student ID Provided");
}
// This dummy function would enroll the student in a course
void Student::enroll(string thisSectionID)
{
    // This first statement updates a variable used to help decide when to      Throw an Exception
    sneakyCount++;
    if (!timeToThrow())
        cout << endl << "Student: " << studentID << " is now enrolled in "     << thisSectionID << endl;
    else
        throw StudentException("Section " + thisSectionID + " has been     cancelled");
    return;
}
// This is the code that helps decide when to throw an exception.  You are     welcome to look at it,
// but its only here to help generate unexpected exceptions.  It will vary     in different versions of Student
// as I play around with it. 
int Student::sneakyCount = 0;
bool Student::timeToThrow()
{
    if (sneakyCount == 4)
        return true;
    else
        return false;
}
#ifndef STUDENTEXCEPTION_H
#define STUDENTEXCEPTION_H
#include <string>
using namespace std;
class StudentException
{
public:
    StudentException(string thisErrorMessage);
    string errorMessage();
private:
    string message;
};
#endif
#include <string>
#include "StudentException.h"
using namespace std;
StudentException::StudentException(string whatWentWrong)
{
    // Set the stored message within the object
    // Any text will be accepted as the error message
    message = whatWentWrong;
}
// Return the error message stored inside the object
string StudentException::errorMessage()
{
    return message;
}

我的测试程序代码:

#include <iostream>
#include <string>
#include "StudentException.h"
#include "Student.h"
using namespace std;
int main()
{
    char again = 'n';
    do
    {
        try
        {
            Student testStudent1("S0000001");
            testStudent1.enroll("CSC-160-500");
        }
        catch(StudentException())
        {
            StudentException testException1("Pre-requisites required");
            cout << testException1.errorMessage();
        }
        cout << "Again?n";
        cin >> again;
    }
    while(tolower(again) == 'y'); 
return 0;
}

我只有更容易测试的循环,因为异常抛出有些随机。如果我使用 catch(...(,我只会捕获异常。关于我做错了什么的任何提示?

catch(StudentException())
{
    StudentException testException1("Pre-requisites required");
    cout << testException1.errorMessage();
}

这不是这样做的方法。您的catch没有捕获实际的异常,您应该将其作为参数:

catch(const StudentException& ex)
{
    cout << ex.errorMessage();
}

catch(StudentException())尝试捕获函数类型。 你想要

catch (StudentException& se)

(然后,您可以在处理程序中使用se,而不是构造新的不相关的StudentException

catch(StudentException()) {
   ...
}

这是错误的语法。你需要说

catch(const StudentException& e) {
    ...
}

当我们在这里时,通常最好从标准库的异常类之一继承异常,例如

class StudentException : public std::runtime_error
{
public:
    StudentException(const string& thisErrorMessage)
        : std::runtime_error(thisErrorMessage)
    {}
};

这不仅更容易实现,而且还提供了 what() 成员函数,人们通常会寻找该函数来查找异常消息。

最新更新