[英]c++:Working with throw,try and catch
我只是想扔掉,试着抓住工作。 这是我的堆栈的头文件,我把我的抛出“/ * * /”暂时忽略它。 http://codepad.org/0Pm2Hy6u
这是因为当我弹出并推送时,如果它是满的则抛出错误或者例外是空的。 我对这些都很陌生。
在我的书中,它将FullStack和EmptyStack设置为...... Class FullStack {}; (所以空类)和EmptyStack一样。
有人可能会帮助我解决这个问题。
这是一个简单的主要内容: http : //codepad.org/dbk4Ke6C
我怎样才能尝试并抓住工作。 ex)当调用stack.Push(item)并且它已满时,我可以捕获错误并显示它
这里已将版本固定为单个文件:
在此处观看: https : //ideone.com/95gMc
简而言之:
using namespace
中的头文件! 对于尝试避免命名空间之间冲突的类的用户,您将使生活变得悲惨 我尽量减少评论,因为在行内引用会有点冗长(IMO应该提请他们加倍注意)
我可以建议:
派生自一个公共堆栈异常基类(也建议一个更一致的异常类命名约定): 编辑修复了一些。 基本原理, 请参阅此背景文章
#include <stdexcept> struct StackException : virtual std::exception { protected: StackException() {} }; struct StackFullException : StackException { char const* what() const throw() { return "StackFullException"; } }; struct StackEmptyException : StackException { char const* what() const throw() { return "StackEmptyException"; } };
这样你就可以随时捕获任何StackException&
(通过引用 )并一次性处理堆栈满/空
要处理异常,请使用类似以下的内容:
int main() { try { // .... } catch (const StackException& e) { std::cerr << "oops, a stack error occured: " << e.what() << std::endl; } }
编辑示例,以演示增强的异常类型和示例处理程序:
//Purpose: Header file for StackType. Containing all declerations and prototypes
#include <stdexcept>
struct StackException : virtual std::exception
{
protected: StackException() {}
};
struct StackFullException : StackException
{
char const* what() const throw() { return "StackFullException"; }
};
struct StackEmptyException : StackException
{
char const* what() const throw() { return "StackEmptyException"; }
};
template <class itemType>
class StackType
{
public:
StackType (int max);
StackType ();
bool IsEmpty() const;
bool IsFull () const;
void Push (itemType newItem);
void Pop ();
itemType Top() const;
~StackType (); // Destructor
private:
int top; // key:top of the stack
int maxStack; // max number of stack items
itemType* list; // pointer to dynamically allocated memory
};
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*Implementation (StackStype.cpp)
StackType prototype functions*/
template <class itemType>
StackType<itemType>::StackType(int max)
{
maxStack = max;
top = -1;
list = new itemType[maxStack];
}
template <class itemType>
StackType<itemType>::StackType()
{
maxStack = 200;
top = -1;
list = new itemType[maxStack];
}
template <class itemType>
bool StackType<itemType>::IsEmpty() const
{
return(top == -1);
}
template <class itemType>
bool StackType<itemType>::IsFull() const
{
return(top == maxStack - 1);
}
template <class itemType>
void StackType<itemType>::Push(itemType newItem)
{
if(IsFull())
{
throw StackFullException();
}
top++;
list[top] = newItem;
}
template <class itemType>
void StackType<itemType>::Pop()
{
if(IsEmpty())
{
throw StackEmptyException();
}
top--;
}
template <class itemType>
itemType StackType<itemType>::Top() const
{
if(IsEmpty())
{
throw StackEmptyException();
}
return list[top];
}
template <class itemType>
StackType<itemType>::~StackType()
{
delete [] list;
}
///////////////////////////////////////
// sample main.cpp
#include <iostream>
int main(void)
{
try
{
StackType<int> stack(5);
stack.Push(5);
stack.Push(2);
stack.Push(3);
stack.Push(4);
stack.Push(1);//<-----Still Ok!
stack.Push(0);//<-----throw FullStack
} catch (const StackException& e)
{
std::cerr << "Received a StackException: what()? " << e.what() << std::endl;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.