Try... Catch... and pre-compiled macros, assert

Source: Internet
Author: User

# Include <iostream> <br/> # include <stdexcept> <br/> # include <cassert> <br/> using namespace STD; <br/> int main (INT argc, char * agrv []) <br/>{< br/> # ifndef ndebug <br/> cerr <"Main start" <Endl <br/> <"line: "<_ line __< <Endl; <br/> # endif <br/> char ch; <br/> while (CIN> CH) <br/>{< br/> assert (Ch! = 'X'); <br/> try <br/>{< br/> If (CH = 'y') <br/> throw runtime_error ("error: shocould not be y "); <br/> cout <" you input correct! You input is: "<ch <Endl; <br/>}< br/> catch (runtime_error ERR) <br/>{< br/> cout <err. what () <Endl <br/> <"Try again? "<Endl; <br/> char C; <br/> CIN> C; <br/> If (CIN & C = 'n ') <br/> break; <br/>}< br/>}

During compilation, you can use: G ++-D ndebug filename. cpp-O filename to specify the macro switch.

Pay special attention to assert, which only plays a role in debugging. The assert statement is ignored for programs compiled in non-debug mode. Therefore, you must never use assert to control your program logic.

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.