簡體   English   中英

c ++:使用throw,try和catch

[英]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

簡而言之:

  • 您需要先定義exeption類,然后才能將它們扔掉。 將它們包括在StackType的頭文件中
  • 不要使用(全球) using namespace中的頭文件! 對於嘗試避免命名空間之間沖突的類的用戶,您將使生活變得悲慘
  • 您需要再增加1個價值

我盡量減少評論,因為在行內引用會有點冗長(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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM