[英]c++:Working with throw,try and catch
I am just trying to get throw, try and catch to work. 我只是想扔掉,试着抓住工作。 Here is my header file for my stack and I place my throw in "/* */" to ignore it for now.
这是我的堆栈的头文件,我把我的抛出“/ * * /”暂时忽略它。 http://codepad.org/0Pm2Hy6u
http://codepad.org/0Pm2Hy6u
It is for when I pop and push so throw out error if it is full or empty with the exception. 这是因为当我弹出并推送时,如果它是满的则抛出错误或者例外是空的。 I am all new at these.
我对这些都很陌生。
In my book it sets FullStack and EmptyStack as so... Class FullStack{}; 在我的书中,它将FullStack和EmptyStack设置为...... Class FullStack {}; (so empty class) and same for EmptyStack.
(所以空类)和EmptyStack一样。
Could some one perhaps help me figure this out. 有人可能会帮助我解决这个问题。
Here is a simple main: http://codepad.org/dbk4Ke6C 这是一个简单的主要内容: http : //codepad.org/dbk4Ke6C
How can I get try and catch to work. 我怎样才能尝试并抓住工作。 ex) When calling stack.Push(item) and it is full I could catch the error and display it
ex)当调用stack.Push(item)并且它已满时,我可以捕获错误并显示它
Here is fixed a version as a single file: 这里已将版本固定为单个文件:
See it live here: https://ideone.com/95gMc 在此处观看: https : //ideone.com/95gMc
In short: 简而言之:
using namespace
in header files! using namespace
中的头文件! You will make life miserable for users of your class that try to avoid clashes between namespaces I minimized the comments because it was a bit lengthy to quote inline (and comments should be pulling their weight, IMO) 我尽量减少评论,因为在行内引用会有点冗长(IMO应该提请他们加倍注意)
May I suggest: 我可以建议:
derive from a common stack exception base class (also suggests a more consistent naming convention for Exception classes): Edit fixed this up somewhat. 派生自一个公共堆栈异常基类(也建议一个更一致的异常类命名约定): 编辑修复了一些。 For rationale, see this background article
基本原理, 请参阅此背景文章
#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"; } };
that way you can always catch any StackException&
(by reference ) and handle either stack full/empty in one go 这样你就可以随时捕获任何
StackException&
(通过引用 )并一次性处理堆栈满/空
to handle the exception, use something like this: 要处理异常,请使用类似以下的内容:
int main() { try { // .... } catch (const StackException& e) { std::cerr << "oops, a stack error occured: " << e.what() << std::endl; } }
Edit example edited to demonstrate the enhanced exception types and a sample handler: 编辑示例,以演示增强的异常类型和示例处理程序:
//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.