Question

C++ : Write a program that demonstrates several exception types being caught with the catch(...) exception...

C++ :

Write a program that demonstrates several exception types being caught with the catch(...) exception handler.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

int main() {
    try {
        throw 123;
    } catch(int n) {
        cout << "caught int " << n << endl;
    } catch(double n) {
        cout << "caught double " << n << endl;
    } catch(char *s) {
        cout << "caught string " << s << endl;
    } catch (...) {
        cout << "caught something unknown" << endl;
    }

    try {
        throw 123.45;
    } catch(int n) {
        cout << "caught int " << n << endl;
    } catch(double n) {
        cout << "caught double " << n << endl;
    } catch(char *s) {
        cout << "caught string " << s << endl;
    } catch (...) {
        cout << "caught something unknown" << endl;
    }

    try {
        throw "hello";
    } catch(int n) {
        cout << "caught int " << n << endl;
    } catch(double n) {
        cout << "caught double " << n << endl;
    } catch(char *s) {
        cout << "caught string " << s << endl;
    } catch (...) {
        cout << "caught something unknown" << endl;
    }

    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ : Write a program that demonstrates several exception types being caught with the catch(...) exception...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • .)   Write a program to implement the exception handling with multiple (at least 3) catch statements....

    .)   Write a program to implement the exception handling with multiple (at least 3) catch statements. Any type of exception may be thrown. Catch statements must display (cout) the type of exception thrown. .)   Write a function which accepts an index and returns the corresponding element from an array. If the index is out of bounds, the function should throw an exception. Handle this exception in "main()". (This is pretty open ended, so anything from a calculator or "what have...

  • please code in java Write a program that illustrates rethrowing an exception. Define methods CISP401Method and...

    please code in java Write a program that illustrates rethrowing an exception. Define methods CISP401Method and CISP401Method2. Method CISP401Method2 should initially throw an exception. Method CISP401Method should call CISP401Method2, catch the exception and rethrow it. Call CISP401Method from method main, and catch the rethrown exception. Print the stack trace of this exception. The following is the result of the program.

  • Consider the following exception types: NullPointerException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException Research what each exception type means and...

    Consider the following exception types: NullPointerException ArrayIndexOutOfBoundsException ClassCastException IllegalArgumentException Research what each exception type means and the conditions under which each occurs (i.e., is thrown). Write programs that demonstrate each type of exception being thrown (one program per exception) and provide a screen capture of the output. Name your programs as follows: NullPointerExceptionThrown, etc. Then, write a program that catches each type of thrown exception and display an error message indicating some of the details as to what that exception...

  • Question 20 Statements that might generate an exception are placed in a catch block. True False...

    Question 20 Statements that might generate an exception are placed in a catch block. True False 1 points Question 21 The class Exception and its subclasses are designed to catch exceptions that should be caught and processed during program execution. True False 1 points Question 22 Which of the following statements is NOT true about creating your own exceptions? Typically, constructors are the only methods that you include when you define your own exception class. The exception class that you...

  • (Catching Exceptions with Superclasses) Use inheritance to create an exception superclass (called ExceptionA) and exception subclasses...

    (Catching Exceptions with Superclasses) Use inheritance to create an exception superclass (called ExceptionA) and exception subclasses ExceptionB and ExceptionC, where ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. Write a program to demonstrate that the catch block for type ExceptionA catches exceptions of types ExceptionB and ExceptionC.

  • (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when...

    (use java) Write a program that creates an exception class called StringTooLongException, designed tobe thrown when a string is discoveredthat has too many characters in it. In the main driver of the program, read strings from the user until the user enters “DONE”. If a string is entered that has too many characters (say 20), throw, catch, and handle the exception. The exception is handled by printing an appropriate message, and the programwill continueprocessing more strings.

  • Read description carefully. Needs to catch exceptions and still be able to keep the program going...

    Read description carefully. Needs to catch exceptions and still be able to keep the program going on past the error. Program should allow user to keep going until he or she quits Math tutor) Write a program that displays a menu as shown in the sample run. You can enter 1, 2. 3, or 4 for choosing an addition. subtraction, multiplication, or division test. After a test is finished, the menu is redisplayed. You may choose another test or enter...

  • 12.5 Program: Divide by Zero (C++) This program will test exception handling during division. The user...

    12.5 Program: Divide by Zero (C++) This program will test exception handling during division. The user will enter 2 numbers and attempt to divide (in a try block) the 1st number by the 2nd number. The program will provide for 2 different catch statements: Division by zero - denominator is zero Division results in a negative number The user should be prompted with Enter 2 numbers: For input of 7 and 0 the output should be Exception: Division by zero...

  • There is a white space in between but nothing is missing it's just the way screenshot...

    There is a white space in between but nothing is missing it's just the way screenshot is taken Can someone help me to solve this question Get Homework Help C PART 1: (7 Marks) Cre PDAssignment 2 Q BOStart Assignment New tab X file:///C:/Users/ksimr/AppData/Local/Packages/Microsoft.MicrosoftEdge_8wekyb3d8bbwe/TempState/Downloads/Assignment%202%20Question%20 PART I: (7 Marks) Create an exception hierarchy of ExceptionA, ExceptionB and ExceptionC such that ExceptionB inherits from ExceptionA and ExceptionC inherits from ExceptionB. 6 Write a test program to show that the catch block for...

  • Placing Exception Handlers File ParseInts.java contains a program that does the following: Prompts for and reads...

    Placing Exception Handlers File ParseInts.java contains a program that does the following: Prompts for and reads in a line of input Uses a second Scanner to take the input line one token at a time and parses an integer from each token as it is extracted. Sums the integers. Prints the sum. Save ParseInts to your directory and compile and run it. If you give it the input 10 20 30 40 it should print The sum of the integers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT