What c ++ exception must know (2)

Source: Internet
Author: User

The logical exceptions provided by the C ++ Standard Library include the following. An invalid_argument exception is thrown when an invalid real parameter is received. Out_of_range: An exception occurs. If a real parameter is not in the expected range, it is thrown. Length_error exception. The report attempts to generate an object with the "Length value exceeding the maximum allowed value. Domain_error is used to report domain errors ). The runtime exceptions provided by the C ++ Standard Library include the following. Range_error: An error occurred while reporting the Range Error in internal calculation. Overflow_error error: An Arithmetic overflow error is reported. Underflow_error error: An Arithmetic overflow error is reported. Bad_alloc exception. This exception is thrown when the new () Operator cannot allocate the required storage zone. It is derived from the base class exception. The first three exceptions are derived from the runtime_error class. Example: # include <iostream> # include <vector> # include <stdexcept> using namespace std; int main () {vector <int> iVec; iVec. push_back (1); iVec. push_back (2); iVec. push_back (3); try {cout <iVec. at (5) <endl;} catch (out_of_range err) {cout <"y ×é ???? "<Endl;} system (" pause ");} integrated classic instance: # include <iostream> using namespace std; class Exception {public: exception (string _ msg = "") {msg = _ msg;} string getMsg () {return msg;} protected: string msg;}; class PushOnFull: public Exception {PushOnFull (string msg = ""): Exception (msg) {}}; class PopOnEmpty: public Exception {public: PopOnEmpty (string msg = ""): exception (msg) {}}; class StackException {public: S TackException (Exception _ e, string _ msg): e (_ e), msg (_ msg) {}exception getSource () {return e;} string getMsg () {return msg;} private: Exception e; string msg;}; template <class T> class iStack {public: iStack (int _ capacity = 64) {capacity = _ capacity; data = new T [capacity]; top = 0 ;}~ IStack () {cout <"~ IStack is called ...... "<endl; delete [] data;} void push (T t) throw (Exception) {if (top> = capacity) {throw Exception ("??? Úá? ");} Data [top] = t; top ++;} T pop () throw (Exception) {if (top <= 0) {throw Exception ("??? A ?? ") ;}Top --; T t = data [top]; return t ;}private: T * data; int capacity; int top ;}; void test () {iStack <int> istack (10); try {for (int I = 0; I <20; I ++) {istack. push (I);} int p = istack. pop (); cout <p <endl;} catch (Exception e) {cout <"2 ??? Á? "<Endl; throw StackException (e," ± ?? A * °? μ? Required bytes 3 bytes ") ;}} void test2 () {iStack <int> istack (10); try {for (int I = 0; I <20; I ++) {istack. push (I);} int p = istack. pop (); cout <p <endl;} catch (...) {cout <"? A ?? Why? ±? D? When? 'Dd μ ?,?? DD × why? 'Quíí ??? <Endl ;}int main () {try {test (); test2 ();} catch (StackException se) {cout <se. getSource (). getMsg () <"" <se. getMsg () <endl;} system ("pause ");}

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.