Custom exception classes in C + +

Source: Internet
Author: User

Header file Stack.h

#ifndef Stack_h
#define Stack_h
#include <exception>
#include <deque>
using namespace Std;
Class Reademptystackerror:public exception
{
Public
Virtual const char * what () const throw ()
{
Return "Error:stack is empty!!";
}
Private


};
Template<class t>
Class Stack
{
Public

void push (const t& elem)
{
C.push_back (Elem);
}
BOOL Empty () const
{
return C.empty ();
}
T pop ()
{
if (empty ())
{
Throw Reademptystackerror ();
}
T Elem (C.back ());
C.pop_back ();
return elem;
}
t& Top ()
{
if (empty ())
{
Throw Reademptystackerror ();
}
return C.back ();
}
Private
std::d eque<t> C;
};
#endif



Source file Main.cpp:

#include <iostream>

#include "Stack.h"
using namespace Std;


void Main () {
Try
{
Stack<int> s;
S.push (3);
S.push (4);
Cout<<s.pop () <<endl;
Cout<<s.pop () <<endl;
Cout<<s.pop () <<endl;
}
catch (Reademptystackerror e)
{
Cout<<e.what () <<endl;
}
}

Custom exception classes in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.