提问人:Chelsea jain 提问时间:9/10/2023 更新时间:9/10/2023 访问量:31
在 C++ 异常处理问题中“抛出”之前出现错误预期初始值设定项
Getting Error expected initializer before 'throw' in C++ Exception Handling Problem
问:
我正在尝试解决黑客等级继承代码问题,并得到我自己无法解决的错误。 我参考了 GFG 来了解异常处理和 bad_exception:https://www.geeksforgeeks.org/exception-handling-c/ https://www.geeksforgeeks.org/exceptionbad_exception-in-c-with-examples/
#include <iostream>
#include <string>
#include <exception>
using namespace std;
void BadLengthException(int n) throw(int) throw(bad_exception)
{
if(n){
throw n;
}
}
bool checkUsername(string username) {
bool isValid = true;
int n = username.length();
if(n < 5) {
throw BadLengthException(n);
}
for(int i = 0; i < n-1; i++) {
if(username[i] == 'w' && username[i+1] == 'w') {
isValid = false;
}
}
return isValid;
}
int main() {
int T; cin >> T;
while(T--) {
string username;
cin >> username;
try {
bool isValid = checkUsername(username);
if(isValid) {
cout << "Valid" << '\n';
} else {
cout << "Invalid" << '\n';
}
} catch (BadLengthException e) {
cout << "Too short: " << e.what() << '\n';
}
}
return 0;
}
答: 暂无答案
评论
throw(/*some type*/)