Q:
C++ Exceptional Handling | Find output programs | Set 1
belongs to collection: C++ find output programs
C++ find output programs
- C++ Operators | Find output programs | Set 1
- C++ Operators | Find output programs | Set 2
- C++ const Keyword | Find output programs | Set 1
- C++ const Keyword | Find output programs | Set 2
- C++ Manipulators | Find output programs | Set 1
- C++ Manipulators | Find output programs | Set 2
- C++ Reference Variable | Find output programs | Set 1
- C++ Reference Variable | Find output programs | Set 2
- C++ Conditional Statements | Find output programs | Set 1
- C++ Conditional Statements | Find output programs | Set 2
- C++ Switch Statements | Find output programs | Set 1
- C++ Switch Statements | Find output programs | Set 2
- C++ goto Statement | Find output programs | Set 1
- C++ goto Statement | Find output programs | Set 2
- C++ goto Statement | Find output programs | Set 2
- C++ Looping | Find output programs | Set 1
- C++ Looping | Find output programs | Set 2
- C++ Looping | Find output programs | Set 3
- C++ Looping | Find output programs | Set 4
- C++ Looping | Find output programs | Set 5
- C++ Default Argument | Find output programs | Set 1
- C++ Default Argument | Find output programs | Set 2
- C++ Arrays | Find output programs | Set 1
- C++ Arrays | Find output programs | Set 2
- C++ Arrays | Find output programs | Set 3
- C++ Arrays | Find output programs | Set 4
- C++ Arrays | Find output programs | Set 5
- C++ Strings | Find output programs | Set 1
- C++ Strings | Find output programs | Set 2
- C++ Strings | Find output programs | Set 3
- C++ Strings | Find output programs | Set 4
- C++ Strings | Find output programs | Set 5
- C++ Class and Objects | Find output programs | Set 1
- C++ Class and Objects | Find output programs | Set 2
- C++ Class and Objects | Find output programs | Set 3
- C++ Class and Objects | Find output programs | Set 4
- C++ Class and Objects | Find output programs | Set 5
- C++ Constructor and Destructor | Find output programs | Set 1
- C++ Constructor and Destructor | Find output programs | Set 2
- C++ Constructor and Destructor | Find output programs | Set 3
- C++ Constructor and Destructor | Find output programs | Set 4
- C++ Constructor and Destructor | Find output programs | Set 5
- C++ this Pointer | Find output programs | Set 1
- C++ this Pointer | Find output programs | Set 2
- C++ this Pointer | Find output programs | Set 3
- C++ Structures | Find output programs | Set 1
- C++ Structures | Find output programs | Set 2
- C++ Structures | Find output programs | Set 3
- C++ Structures | Find output programs | Set 4
- C++ Structures | Find output programs | Set 5
- C++ Friend Function | Find output programs | Set 1
- C++ Friend Function | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 1
- C++ Function Overloading | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 2
- C++ Function Overloading | Find output programs | Set 3
- C++ Static Variables and Functions | Find output programs | Set 1
- C++ Static Variables and Functions | Find output programs | Set 2
- C++ Static Variables and Functions | Find output programs | Set 3
- C++ Static Variables and Functions | Find output programs | Set 4
- C++ Static Variables and Functions | Find output programs | Set 5
- C++ Operator Overloading | Find output programs | Set 1
- C++ Operator Overloading | Find output programs | Set 2
- C++ Operator Overloading | Find output programs | Set 3
- C++ Operator Overloading | Find output programs | Set 4
- C++ Operator Overloading | Find output programs | Set 5
- C++ Enumeration | Find output programs
- C++ Inheritance | Find output programs | Set 1
- C++ Inheritance | Find output programs | Set 2
- C++ Inheritance | Find output programs | Set 3
- C++ Inheritance | Find output programs | Set 4
- C++ Inheritance | Find output programs | Set 5
- C++ Exceptional Handling | Find output programs | Set 1
- C++ Exceptional Handling | Find output programs | Set 2
- C++ Exceptional Handling | Find output programs | Set 3
- C++ Virtual Functions | Find output programs | Set 1
- C++ Virtual Functions | Find output programs | Set 2
- C++ Virtual Functions | Find output programs | Set 3
- C++ Templates | Find output programs | Set 1
- C++ Templates | Find output programs | Set 2
- C++ Preprocessor | Find output programs | Set 1
- C++ Preprocessor | Find output programs | Set 2
- C++ Preprocessor | Find output programs | Set 3
- C++ Signal Handling | Find output programs | Set 1
- C++ Namespace | Find output programs | Set 1
- C++ Namespace | Find output programs | Set 2
- C++ Dynamic Memory Allocation | Find output programs | Set 1
- C++ Dynamic Memory Allocation | Find output programs | Set 2
- C++ Dynamic Memory Allocation | Find output programs | Set 3
Answer Program 1:
Output:
Explanation:
Here, we created two code blocks "try" and "catch". We declared 3 local variables in num1, num2, and res.
Here, variable num1 is initialized with 10 and num2 initialized with 0 then the below expression will generate a runtime exception:
res = num1/num2;
Because the above expression will generate the situation of "Divide By Zero". But here we did not use "throw" to throw the exception, that's why the program will crash at runtime.
Answer Program 2:
Output:
Explanation:
Here, we created two code blocks "try" and "catch". We declared 3 local variables in num1, num2, and res.
Here, variable num1 is initialized with 10 and num2 initialized with 0 then the below expression will generate a runtime exception:
res = num1/num2;
But here we handled the exception; if num2 is 0 then defined error code will be thrown and caught by "catch" block.
In the catch block, we printed the received exception code using cout.
Answer Program 3:
Output:
Explanation:
The above crashed at runtime because here we have thrown a constant string ("Divide By Zero") using "throw" keyword, and using char *exp in the "catch" block.
To resolve the problem we need to use const char *exp instead of char *exp.
Answer Program 4:
Output:
Explanation:
Here, we defined a function funDiv() with two arguments X and Y. Here, we checked if the value of Y is 0 then it will throw a string message using the "throw" keyword.
Now coming to the main() function, here we declared local variable num1 and num2 inside the “try” block, and call funDiv() function to perform division.
The string message is thrown by funDiv() function, caught by the "catch" block, and print the message using cout on the console screen.
Answer Program 5:
Output:
Explanation:
Here, we defined a function funDiv() with two arguments X and Y. Here, we checked if the value of Y is 0 then it will throw an object of exception class using the "throw" keyword.
Now coming to the main() function, here we declared local variable num1 and num2 inside the “try” block, and call funDiv() function to perform division.
The exception object was thrown by funDiv() function, caught by the "catch" block, and print the message using cout on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer