Not logged in. Log in with GitHub. About Websheets.

List of all available Websheets


Viewing cpp/cs104/exceptions/divide_std_except by redekopp@usc.edu. You have unsaved changes. Log in above to create, edit, preview and publish Websheets.
PropertyValue
Description
html markup
shown to student
 
Replace the throwing and catching of primitive types to use the standard exception <tt>invalid_argument</tt>.
<p>
   When you throw it, provide the error message <tt>Denom is 0</tt> to its constructor.
<p>
   Then in the catch block print out the error message by calling the <tt>what()</tt> member function which will return the error string.
   
Remarks
Comments, history, license, etc.
 
Copied from problem cpp/cs104/exceptions/divide_throw_catch_main (author: redekopp@usc.edu)
Copied from problem cpp/cs104/exceptions/divide_throw_catch_primitive (author: redekopp@usc.edu)
Copied from problem cpp/cs104/exceptions/divide_throw_only (author: redekopp@usc.edu)
Copied from problem cpp/cs104/exceptions/divide_assert (author: redekopp@usc.edu)
Copied from problem cpp/cs104/exceptions/divide1 (author: redekopp@usc.edu)
remove
Engine
Template / Reference solution
 
#include <iostream>
#include <string>
#include <stdexcept>
using namespace std;
int divide(int num, int denom)
{
   if(denom == 0){
      \[
REDACTED
]\
   }
   cout << "Normal case" << endl;
   return(num/denom);
}
int f1(int x)
{
   return divide(x, x-2);       
}
int main()
{
   int res = -1, a;
   cout << "Enter: " << endl;
   cin >> a;
   // Add code to try and catch exception cases
   //  and if an exception is caught, try to get another
   //  input from the user until they enter a value that
   //  does not generate an exception.
   while(true){
      try {
        res = f1(a);
        // I'll only get here if no exception occurred
        break;
      }
    catch(\[ REDACTED ]\){
        \[ REDACTED ]\
        cin >> a;
      }
   }
   cout << res << endl;
   return 0;
}
C++ test suite
json list of stdin/args tests
e.g. [{"stdin":"hi", "args":["4", "5"]},
{"stdin":"noargs"}]

to just run once with no input use [{}]
 
[{"stdin":"2 4"} ]
Solution visibility remove


Optional properties:

Note: problems are open-source by default (see 'Public permissions'). Assumed license for open problems is Creative Commons 4.0 Attribution-ShareAlike unless specified in 'Remarks'.