"C + +" stack empty stack full exception handling __c++

Source: Internet
Author: User
Tags exception handling throw exception

Mechanism of exception handling:

1. If no exception occurs, the code in the try block continues to execute, the catch clause associated with the TRY block is ignored, the program executes normally, and main () returns 0.
2. When the first try block throws an exception in the For loop, the for loop exits and the try block exits to execute the catch clause for the pushonfull exception. Istack. Printstack () is no longer executed and is ignored.
3. If the second try block calls pop () to throw an exception, exit the for and try block to perform a catch clause for the poponempty exception.
4. When a statement throws an exception, the statement following the statement is skipped. The execution of the program is given to the catch clause that handles the exception, and if no catch clause can handle the exception, the terminate () defined in the C + + standard library is given.

The specific exception handling code is as follows:

#include <iostream> #include <iomanip> #include <string> using namespace std; Exception class Template<class type> class Pushonfull//Stack full exception {Public:pushonfull (string s): Str (s) {} Ty
	PE GetData () const {return data;
	string what ()//Output prompt statement {return str;
	BOOL Remalloc () {return true;
} private:string str;

}; Template<class type> class Poponempty//Stack null exception {Public:poponempty (string s): Str (s) {} string
	What () const//Output prompt statement {return str;
} private:string str;

}; Template<class type> class Stack//stack {public:stack (int sz=size) {capacity = Sz>s
		Ize?sz:size;
		base = new Type[capacity];
	top = 0;
		} ~stack () {delete []base;
	base = NULL;
	} public:bool Isfull () const//judge whether it is full {return top >= capacity;
	BOOL IsEmpty () const//determine if NULL {return top = 0; } void Push (const Type; x)//into stack {if (Isfull ()) {string str ("stack full stack exception occurs.")
			");  Throw pushonfull<type> (str);
	Throw exception} base[top++] = x;
			void Pop ()//out stack {if (IsEmpty ()) {string str = "Stack empty out stack exception!";  Throw poponempty<type> (str);
	Throw exception} top--;
	} private:enum{size=8};
	Type *base;
	size_t capacity;
int top;

};
	void Main () {stack<int> st; Try//exception handling {for (int i=1; i<=5; ++i) {St.
		Push (i);
	} catch (Pushonfull<int> &e)//catch Exception {Cout<<e.what () <<endl; Try//exception handling {for (int i=0; i<10; ++i) {St.
		Pop ();
	} catch (Poponempty<int> &exp)//catch Exception {Cout<<exp.what () <<endl; }
}

What's the problem I hope you can point out, thank you all ~

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.