codeGK
Code it ALL
C++ Try Except - CodeGK
♦ C++ Try Except

♣ About C++ Try Except

Try and Except paradigm in programming is mainly used to avoid errors, this can be done by raising Exceptions.
Many types of exceptions can be thrown, such as error exception, invalid exception, compilation exception, etc.

♣ C++ Try Except Syntax

Below is the representation of C++ Try Except Syntax.
Basic Syntax
try { //code_block for trying with throwing exception } catch () { //code_block if error is encountered }
Syntax Explanation:
try: try a bunch of code lines catch: block of to be executed if previous try block throws error

♣ Example1: Verifying age in C++

In this example, Person with age 14 is restricted from entering the PUB.
Example
Input:
Output:

♣ Example2: Checking Id in C++

In this example, a trespasser will be prosecuted.
Example
Input:
Output:
Prev_LessonNext_Lesson
C++ Try Except