Question

In addition to including an exceptions library, you can also create exceptions by using the throw...

In addition to including an exceptions library, you can also create exceptions by using the throw keyword.

(1pt) Create a function called int throwsInt() that will throw an integer

(1pt) Create a function called double throwsDouble() that will throw a double

(2pt) Using try/catch statements, expand the code stubs to call each function in it's own try block, and catch and report the thrown value.

Provided code:

#include <iostream>

using namespace std;

int throwsInt();

double throwsDouble();

int main() {
cout << "about to run throwsInt()" << endl;

try {
int val = throwsInt();
cout << "val: " << val << endl;
}
catch (int i) {
cout << "Caught integer: " << i << endl;
}

cout << "about to run throwsDouble()" << endl;

// Build your try/catch based on the example above!
}

int throwsInt() {
//implement this function here
}

double throwsDouble() {
//implement this function here
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <exception>

using namespace std;

int throwsInt();
double throwsDouble();

int main()
{
cout << "about to run throwsInt()" << endl;
  
try
{
//method calling
int val = throwsInt();
  
//display value
cout << "val: " << val << endl;
}
catch (int i)
{
cout << "Caught integer: " << i << endl;
}
  
cout << "about to run throwsDouble()" << endl;
try
{
//method calling
double val = throwsDouble();
  
//display value
cout << "val: " << val << endl;
}
catch (double i)
{
cout << "Caught integer: " << i << endl;
}
}

int throwsInt()
{
//return statement
return 10;
}

double throwsDouble()
{
//return statement
return 20.00;
}

Screenshot of the source code:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
In addition to including an exceptions library, you can also create exceptions by using the throw...
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
  • Background: The purpose of this assignment is to practice dealing with exception handling and textual data....

    Background: The purpose of this assignment is to practice dealing with exception handling and textual data. Exception handling is a very important part of being an object-oriented programming. Rather returning some kind of int return value every time you tickle an object, C++ programmers expect methods to focus on their task at hand. If something bad happens, C++ programmers expect methods to throw exceptions. When caught, exceptions can be processed. When uncaught, they cause a program to terminate dead in...

  • Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing an...

    Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...

  • Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming....

    Background: This assignment deals with inheritance. Inheritance is one of the major principles of object-oriented programming. In C++, one of the biggest goals is "code reuse". Inheritance accomplishes this. In order to get inheritance working in C++, you must get both the structure of your .h files as well as the implementation of your constructor correct. Constructor implementations must use an initialization list. Please review the book and the online content on these issues so you know how it works...

  • Hello everyone! I am working on my assignment for C++ and I'm having a bit of...

    Hello everyone! I am working on my assignment for C++ and I'm having a bit of trouble on the value producing functions, I am creating a program wherein I have to input a value and I need to create a function called square value where in it will square the value I have input. I cannot seem to figure out this part. Here is the code that I have so far, and thank you for any help. I greatly appreciate...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • C++ Write a function so that the main() code below can be replaced by the simpler...

    C++ Write a function so that the main() code below can be replaced by the simpler code that calls function MphAndMinutesToMiles(). Original main(): int main() { double milesPerHour; double minutesTraveled; double hoursTraveled; double milesTraveled; cin >> milesPerHour; cin >> minutesTraveled; hoursTraveled = minutesTraveled / 60.0; milesTraveled = hoursTraveled * milesPerHour; cout << "Miles: " << milesTraveled << endl; return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 #include <iostream> using...

  • //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what)...

    //stack_exception.h #ifndef STACK_EXCEPTION #define STACK_EXCEPTION #include <string> using namespace std; class Stack_Exception { public: Stack_Exception(string what) : what(what) {} string getWhat() {return what;} private: string what; }; #endif //stack_test_app.cpp #include <iostream> #include <sstream> #include <string> #include "stack.h" using namespace std; /********************************************* * The 'contains' function template goes here * *********************************************/ int main() {    cout << boolalpha;    cout << "--- stack of int" << endl;    Stack<int> si;    cout << "si intially " << si << endl;   ...

  • This is in C++. It is a program demonstrating how a class can be used to...

    This is in C++. It is a program demonstrating how a class can be used to modify a box in different ways/throw invalid arguments. I am having trouble understanding the code as the teacher did not include comments, can someone please add some detailed ones to help? #include<iostream> #include<stdexcept> using namespace std; class Box { double height, width, depth; public:     Box() { height = width = depth = 1.0; } Box(double h, double w, double d) { height =...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

    Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...

  • #include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left;...

    #include <iostream> #include <cstddef> using std::cout; using std::endl; class Node { int value; public: Node* left; // left child Node* right; // right child Node* p; // parent Node(int data) { value = data; left = NULL; right = NULL; p = NULL; } ~Node() { } int d() { return value; } void print() { std::cout << value << std::endl; } }; int main(int argc, const char * argv[]) { } function insert(Node *insert_node, Node *tree_root){ //Your code here...

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