我对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(...),我只会捕获异常。关于我做错了什么的暗示?
答案 0 :(得分:1)
catch(StudentException())
尝试捕获函数类型。你想要
catch (StudentException& se)
(然后您可以在处理程序中使用se
,而不是构建新的无关StudentException
。)
答案 1 :(得分:1)
catch(StudentException()) {
...
}
这是错误的语法。你需要说
catch(const StudentException& e) {
...
}
虽然我们在这里,但从standard library's exception classes之一继承例外通常是一个好主意,例如
class StudentException : public std::runtime_error
{
public:
StudentException(const string& thisErrorMessage)
: std::runtime_error(thisErrorMessage)
{}
};
这不仅更容易实现,而且还提供了what()
成员函数,人们通常会寻找该函数来查找异常消息。
答案 2 :(得分:1)
catch(StudentException())
{
StudentException testException1("Pre-requisites required");
cout << testException1.errorMessage();
}
这不是这样做的方法。你的catch
没有捕捉到实际的异常,你应该把它作为一个论点:
catch(const StudentException& ex)
{
cout << ex.errorMessage();
}