C++ 如何使用try-and-catch中止函数? void AddNewPerson(string&firstName、string&lastName、string&id) { 试一试{ 不能直呼其名; 抛出名字; 库特姓氏; 抛出姓氏; cout-id; 投掷id; } 捕获(字符x){ 如果(x='a'){ //离开这里 } } }
每当用户想要使用char'a'中止时,我都会尝试退出该函数,但我不确定如何使用try和catch语句。有什么帮助吗?您想做的可能是:C++ 如何使用try-and-catch中止函数? void AddNewPerson(string&firstName、string&lastName、string&id) { 试一试{ 不能直呼其名; 抛出名字; 库特姓氏; 抛出姓氏; cout-id; 投掷id; } 捕获(字符x){ 如果(x='a'){ //离开这里 } } },c++,C++,每当用户想要使用char'a'中止时,我都会尝试退出该函数,但我不确定如何使用try和catch语句。有什么帮助吗?您想做的可能是: void AddNewPerson(string &firstName, string &lastName, string &id) { try{ cout <<"Enter first name (a to abort): "; cin >> firstName;
void AddNewPerson(string &firstName, string &lastName, string &id)
{
try{
cout <<"Enter first name (a to abort): ";
cin >> firstName;
throw firstName;
cout <<"Enter last name (a to abort): ";
cin >> lastName;
throw lastName;
cout <<"Enter ID Number (a to abort): ";
cin >> id;
throw id;
}
catch(char x){
if(x == 'a'){
//exit here
}
}
}
class abort_exception : public exception{}
//....
void AddNewPerson( string & firstName, string & lastName, string & id )
{
try
{
cout << "Enter first name (a to abort): ";
cin >> firstName;
if( firstName == "a" )
throw abort_exception();
// ...
}
catch( abort_exception& e )
{
cerr << "User Aborted.";
}
}
cout firstName;
如果(firstName==“a”)抛出firstName;
如果没有
,整个事情就没有意义,因为throw
之后的下一行永远不会执行。return
退出函数。但是,我看不出除了第一次抛出之外,您将如何到达这些行。void AddNewPerson(string&firstName、string&lastName、string&id)
cout <<"Enter first name (a to abort): ";
cin >> firstName;
if (firstName == "a") throw firstName;
{
试一试{
不能直呼其名;
如果(名字=“a”)
抛出名字;
库特姓氏;
如果(lastName==“a”)
抛出姓氏;
cout-id;
如果(id=“a”)
投掷id;
}
catch(stringx){//catch用于例外。
如果(x=“a”){
//离开这里
回来
}
}
}
正如其他人所说,您应该抛出一个异常的实例(或一个子类)
可能有必要在更广泛的背景下说明发生了什么。假设有一个循环多次调用函数:
// returns true on success, false on failuer
bool AddNewPerson( string & firstName, string & lastName, string & id )
{
try
{
// ...
}
catch( abort_exception& e )
{
cerr << "User Aborted.";
return false;
}
return true;
}
您可以使用异常来中断循环:
while (true) // endless loop
{
string firstName, lastName, id;
AddNewPerson(string &firstName, string &lastName, string &id);
...
}
// Do something else
如果使用此构造,请按以下方式更改输入函数:
try {
while (true) // endless loop
{
string firstName, lastName, id;
AddNewPerson(string &firstName, string &lastName, string &id);
...
}
}
catch(EndOfInput x)
{
// Nothing is actually exceptional here: the user just finished their input
}
// Do something else
class EndOfInput:public std::exception{};
void AddNewPerson(string和firstName、string和lastName、string和id)
{
不能直呼其名;
如果(名字=“a”)
抛出EndOfInput();
库特姓氏;
如果(lastName==“a”)
抛出EndOfInput();
cout-id;
如果(id=“a”)
抛出EndOfInput();
//这里什么都不用抓
}
注意:建议抛出std::exception
的子类,而不是像string
这样的一般类,以避免一些混淆。您可以使用throw
抛出异常,但是如果您想因为它而离开函数,您应该确保在实际退出函数之前不会捕获它。这就是你在代码中所做的;您无条件地抛出异常,然后立即测试抛出的值
如果您只希望在用户输入特定内容时退出,那么请确保在启动离开函数的过程之前检查该特殊值。也就是说,在检查值之前不要抛出:
class EndOfInput: public std::exception {};
void AddNewPerson(string &firstName, string &lastName, string &id)
{
cout <<"Enter first name (a to abort): ";
cin >> firstName;
if (firstName == "a")
throw EndOfInput();
cout <<"Enter last name (a to abort): ";
cin >> lastName;
if (lastName == "a")
throw EndOfInput();
cout <<"Enter ID Number (a to abort): ";
cin >> id;
if (id == "a")
throw EndOfInput();
// No need to catch anything here
}
cin>>值;
如果(){
投
}
如果等到捕捉到异常后再检查值,则为时已晚。一旦抛出,就不能返回到抛出的位置并继续运行,以防用户没有输入特殊的中止指示器
您的指导老师可能希望您捕获函数外部的异常,即调用方中的异常。根据在函数外部执行的代码行,您将知道是否已获得所需的所有信息:
cin >> value;
if (<value-is-special>) {
throw <something>;
}
试试看{
AddNewPerson(…);
//如果你到了这里,你就有了关于一个新人的所有数据。
}捕获(){
//如果您到达这里,用户将中止。
}
您抛出的对象类型应该与catch
子句中指定的类型匹配。在您的代码中,您正在抛出读取输入的变量。这很好,但是因为这些变量是字符串,所以捕获的类型也应该是string
。catch(char)
子句是合法的,但不会捕获抛出的string
,因为它们不是相同的类型,即使它们包含类似类型的值
提前离开函数的更典型的方法是使用return
,但是如果您的作业要求您使用异常,那就这样吧。异常是针对异常情况的,而不是针对控制流。我的教授要求我们在这种情况下使用try-and-catch。项目的要求是,用户应该能够在任何步骤退出,我们应该使用try…catch来处理这个问题。不应该catch(char x)
是catch(int x)
?考虑到代码是抛出字符串,它应该是catch(std::string&x)
:但这本身就是个坏主意;你真的应该抛出并捕获一个std::exception
的子类,除非你有很好的理由这样做。你的教授用这个例子来教try/catch时需要挨揍。据说整个要点就是使用异常。(为什么教授会选择一个不应该使用异常的情况,这是无关紧要的。)我不知道:),try-and-catch占用了这么多内存,在这个函数中是没有意义的,我不知道谁是profesor,这不会编译try
必须至少有一个catch
处理程序。更不用说std::string
提供了operator==
加上您正在抛出类型std::string
的异常,并捕获类型char
。当调用throw(id)
时,您认为会发生什么?编辑以使用throws。
cin >> value;
if (<value-is-special>) {
throw <something>;
}
try {
AddNewPerson(...);
// If you get here, you have all the data about a new person.
} catch (<type>) {
// If you get here, the user aborted.
}