C++ Try/Catch&;抛掷器不能正常工作
我似乎无法使我的尝试/捕获正确工作。当您实现try/catch时,它应该“抛出”您告诉它的任何字符串,对吗?如果你愿意,让程序继续。我的没有说我想说的,也没有继续,相反,它告诉我,然后中止: 调试错误!!诸如此类 R6010-已调用abort()(按Retry调试应用程序) 我想让它说:“你正在尝试添加超出允许范围的项目。不要添加。”然后继续程序。它是一个LinkedList,不允许它有超过30个节点。当它试图添加超过30个时,它确实停止了,只是不是我想要的那样。我不确定我做错了什么,非常感谢你的帮助C++ Try/Catch&;抛掷器不能正常工作,c++,class,templates,linked-list,try-catch,C++,Class,Templates,Linked List,Try Catch,我似乎无法使我的尝试/捕获正确工作。当您实现try/catch时,它应该“抛出”您告诉它的任何字符串,对吗?如果你愿意,让程序继续。我的没有说我想说的,也没有继续,相反,它告诉我,然后中止: 调试错误!!诸如此类 R6010-已调用abort()(按Retry调试应用程序) 我想让它说:“你正在尝试添加超出允许范围的项目。不要添加。”然后继续程序。它是一个LinkedList,不允许它有超过30个节点。当它试图添加超过30个时,它确实停止了,只是不是我想要的那样。我不确定我做错了什么,非常感谢你
Main:
Collection<int> list;
for(int count=0; count < 31; count++)
{
try
{
list.addItem(count);
cout << count << endl;
}
catch(string *exceptionString)
{
cout << exceptionString;
cout << "Error";
}
}
cout << "End of Program.\n";
Collection.h:
template<class T>
void Collection<T>::addItem(T num)
{
ListNode<T> *newNode;
ListNode<T> *nodePtr;
ListNode<T> *previousNode = NULL;
const std::string throwStr = "You are trying to add more Items than are allowed. Don't. ";
// If Collection has 30 Items, add no more.
if(size == 30)
{
throw(throwStr);
}
else
{}// Do nothing.
// Allocate a new node and store num there.
newNode = new ListNode<T>;
newNode->item = num;
++size;
// Rest of code for making new nodes/inserting in proper order
// Placing position, etc etc.
}
Main:
收集清单;
对于(int count=0;count<31;count++)
{
尝试
{
列表.附加项(计数);
cout您正在抛出一个字符串,但试图捕获指向字符串的指针
将try/catch块更改为:
try
{
...
}
catch( const string& exceptionString )
{
cout << exceptionString;
}
试试看
{
...
}
捕获(常量字符串和例外字符串)
{
cout您正在抛出一个字符串,但试图捕获指向字符串的指针
将try/catch块更改为:
try
{
...
}
catch( const string& exceptionString )
{
cout << exceptionString;
}
试试看
{
...
}
捕获(常量字符串和例外字符串)
{
库特