Question

In: Computer Science

Exceptions in C++ Tasks Verify that the EXECUTE_BLOCK symbol is set to TRUE in the csc232.h...

Exceptions in C++

Tasks

  1. Verify that the EXECUTE_BLOCK symbol is set to TRUE in the csc232.h header file.
  2. Follow the TODO: instructions in Main.cpp.
  3. Look for the TODO: comments in csc232.h. In essence, you'll be creating your custom exception class, and then throwing that custom exception from within another function. You'll also toggle the corresponding macro to TRUE in order to validate your work with a Google Unit Test.

//csc232.h

#ifndef CSC232_LAB07_H
#define CSC232_LAB07_H

#define FALSE 0
#define TRUE 1
#define EXECUTE_BLOCK TRUE
// TODO: When you create your pull request, make sure all of the following are set to TRUE
#define TEST_INVALID_TARGET_EXCEPTION FALSE
#define TEST_UNEXPECTED_ERROR_EXCEPTION FALSE
#define TEST_INVALID_RANGE_EXCEPTION FALSE

#include <stdexcept>
#include <string>

/**
 * @brief Custom exception to be thrown whenever a logical error is encountered.
 */
class TargetNotFoundException : public std::logic_error {
public:
    /**
     * @brief Initializing constructor.
     * @param message an informative message to include with this logic_error
     */
    explicit TargetNotFoundException(const std::string &message = "")
            : std::logic_error("Target not found: " + message) {
        // Intentionally empty as initialization utilizes an initializer list
    }
};

// TODO: Create a custom exception class named InvalidTargetException that extends std::invalid_argument

// TODO: Create a custom exception class named UnexpectedErrorException that extends std::runtime_error

// TODO: Create a custom exception class named InvalidRangeException that extends std::range_error

/**
 * @brief A demonstration of throwing the TargetNotFoundException.
 * @param target the target that wasn't found during processing
 */
void ThrowTargetNotFoundException(const std::string& target) {
    throw TargetNotFoundException(target);
}

/**
 * @brief A demonstration of throwing the InvalidTargetException.
 * @param error the error encountered during processing
 */
void ThrowInvalidTargetException(const std::string& error) {
    // TODO: throw an InvalidTargetException with the given error message

    // TODO: when done, change TEST_INVALID_TARGET_EXCEPTION to TRUE and commit your changes
}

/**
 * @brief A demonstration of throwing the UnexpectedErrorException.
 * @param error the error encountered during processing
 */
void ThrowUnexpectedErrorException(const std::string& error) {
    // TODO: throw an UnexpectedErrorException with the given error message

    // TODO: when done, change TEST_UNEXPECTED_ERROR_EXCEPTION to TRUE and commit your changes
}

/**
 * @brief A demonstration of throwing the InvalidRangeException.
 * @param error the error encountered during processing
 */
void ThrowInvalidRangeException(const std::string& error) {
    // TODO: throw an InvalidRangeException with the given error message

    // TODO: when done, change TEST_INVALID_RANGE_EXCEPTION to TRUE and commit your changes
}

#endif //CSC232_LAB07_H

//main.cpp

#include <cstdlib>
#include <iostream>
#include <string>
#include "csc232.h"

/**
 * @brief A contrived function that always throws an exception
 * @param target presumably something this function seeks to find
 * @return true
 * @throws ThrowTargetNotFoundException always!
 */
bool findSomeTarget(const std::string& target) {
    ThrowTargetNotFoundException(target);
    return true;
}

/**
 * @brief A simple demonstration on how to handle exceptions and gracefully exit.
 * @return EXIT_SUCCESS upon successfully handling exceptional code.
 */
int main()
{
    std::cout << "A program doomed to failure..." << std::endl << std::endl;
    bool targetDoesNotExist{false};
    std::string target;

    // TODO: Run this main target with EXECUTE_BLOCK initially set to true and observe the exit code
#if EXECUTE_BLOCK
    std::cout << "Enter a target value to search: ";
    std::cin >> target;
    targetDoesNotExist = findSomeTarget(target);
#else
    // TODO: Toggle EXECUTE_BLOCK to false, run this target again and observe the exit code
    try {
        std::cout << "Enter a target value to search: ";
        std::cin >> target;
        targetDoesNotExist = findSomeTarget(target);
    } catch (const TargetNotFoundException& notFoundException) {
        std::cerr << notFoundException.what() << std::endl;
        targetDoesNotExist = true;
    }
    if (targetDoesNotExist) {
        std::cout << "I told you so... this program was doomed to fail!" << std::endl;
        std::cout << "But at least it did so gracefully ;-)" << std::endl << std::endl;
    }
#endif
    return EXIT_SUCCESS;

Solutions

Expert Solution

The rest of exceptions were added (required as part of TODOs) in similar fashion.

Also using the MACROS defined in header file, I added new code segments in the main class to call exception specific class in header file to test each scenarios. 1) With only throwing exception. 2) Handling exception by using try catch.

The files are provided below in code blocks.

#ifndef CSC232_LAB07_H
#define CSC232_LAB07_H

#define FALSE 0
#define TRUE 1
#define EXECUTE_BLOCK FALSE
// TODO: When you create your pull request, make sure all of the following are set to TRUE
#define TEST_INVALID_TARGET_EXCEPTION FALSE
#define TEST_UNEXPECTED_ERROR_EXCEPTION FALSE
#define TEST_INVALID_RANGE_EXCEPTION FALSE

#include <stdexcept>
#include <string>

/**
 * @brief Custom exception to be thrown whenever a logical error is encountered.
 */
class TargetNotFoundException : public std::logic_error {
public:
    /**
     * @brief Initializing constructor.
     * @param message an informative message to include with this logic_error
     */
    explicit TargetNotFoundException(const std::string &message = "")
            : std::logic_error("Target not found: " + message) {
        // Intentionally empty as initialization utilizes an initializer list
    }
};

// TODO: Create a custom exception class named InvalidTargetException that extends std::invalid_argument
/**
 * @brief Custom exception to be thrown whenever invalid target is encountered
 */
class InvalidTargetException : public std::logic_error {
public:

    explicit InvalidTargetException(const std::string &message = "")
            : std::logic_error("Invalid target: " + message) {

    }
};

// TODO: Create a custom exception class named UnexpectedErrorException that extends std::runtime_error
/**
 * @brief Custom exception to be thrown whenever unexpected exception is encountered
 */
class UnexpectedErrorException : public std::logic_error {
public:

    explicit UnexpectedErrorException(const std::string &message = "")
            : std::logic_error("Unexpected error: " + message) {

    }
};


// TODO: Create a custom exception class named InvalidRangeException that extends std::range_error
/**
 * @brief Custom exception to be thrown whenever unexpected range is encountered
 */
class InvalidRangeException : public std::logic_error {
public:

    explicit InvalidRangeException(const std::string &message = "")
            : std::logic_error("Invalid range: " + message) {

    }
};

/**
 * @brief A demonstration of throwing the TargetNotFoundException.
 * @param target the target that wasn't found during processing
 */
void ThrowTargetNotFoundException(const std::string& target) {
    throw TargetNotFoundException(target);
}

/**
 * @brief A demonstration of throwing the InvalidTargetException.
 * @param error the error encountered during processing
 */
void ThrowInvalidTargetException(const std::string& error) {
    // TODO: throw an InvalidTargetException with the given error message
    throw InvalidTargetException(error);
    // TODO: when done, change TEST_INVALID_TARGET_EXCEPTION to TRUE and commit your changes
}

/**
 * @brief A demonstration of throwing the UnexpectedErrorException.
 * @param error the error encountered during processing
 */
void ThrowUnexpectedErrorException(const std::string& error) {
    // TODO: throw an UnexpectedErrorException with the given error message
    throw UnexpectedErrorException(error);
    // TODO: when done, change TEST_UNEXPECTED_ERROR_EXCEPTION to TRUE and commit your changes
}

/**
 * @brief A demonstration of throwing the InvalidRangeException.
 * @param error the error encountered during processing
 */
void ThrowInvalidRangeException(const std::string& error) {
    // TODO: throw an InvalidRangeException with the given error message
    throw InvalidRangeException(error);
    // TODO: when done, change TEST_INVALID_RANGE_EXCEPTION to TRUE and commit your changes
}

#endif //CSC232_LAB07_H
#include <cstdlib>
#include <iostream>
#include <string>
#include "csc232.h"

/**
 * @brief A contrived function that always throws an exception
 * @param target presumably something this function seeks to find
 * @return true
 * @throws ThrowTargetNotFoundException always!
 */
bool findSomeTarget(const std::string& target) {
    ThrowTargetNotFoundException(target);
    return true;
}


/**
 * @brief A simple demonstration on how to handle exceptions and gracefully exit.
 * @return EXIT_SUCCESS upon successfully handling exceptional code.
 */
int main() {
    std::cout << "A program doomed to failure..." << std::endl << std::endl;
    bool targetDoesNotExist{false};
    std::string target;

    // TODO: Run this main target with EXECUTE_BLOCK initially set to true and observe the exit code
#if EXECUTE_BLOCK
    std::cout << "Enter a target value to search: ";
    std::cin >> target;
    targetDoesNotExist = findSomeTarget(target);
#else
    // TODO: Toggle EXECUTE_BLOCK to false, run this target again and observe the exit code
    try {
        std::cout << "Enter a target value to search: ";
        std::cin >> target;
        targetDoesNotExist = findSomeTarget(target);
    } catch (const TargetNotFoundException &notFoundException) {
        std::cerr << notFoundException.what() << std::endl;
        targetDoesNotExist = true;
    }
    if (targetDoesNotExist) {
        std::cout << "I told you so... this program was doomed to fail!" << std::endl;
        std::cout << "But at least it did so gracefully ;-)" << std::endl << std::endl;
    }
#endif


    // test invalid target exception
#if  TEST_INVALID_TARGET_EXCEPTION
    ThrowInvalidTargetException("the target is invalid");
#else
    try {
        ThrowInvalidTargetException("the target is invalid");
    } catch(const InvalidTargetException& exception ) {
        std::cerr << exception.what() << std::endl;
        std::cout<< "Handled Invalid target exception gracefully" <<std::endl;
    }
#endif

    // test unexpected exception.
#if TEST_UNEXPECTED_ERROR_EXCEPTION
    ThrowUnexpectedErrorException("Unexpected exception");
#else
    try {
        ThrowUnexpectedErrorException("Unexpected exception");
    } catch(const UnexpectedErrorException& exception) {
        std::cerr << exception.what() << std::endl;
        std::cout<< "Handled Unexpected exception gracefully" <<std::endl;
    }
#endif

    // test invalid range
#if TEST_INVALID_RANGE_EXCEPTION
    ThrowInvalidRangeException("Invalid range");
#else
    try {
        ThrowInvalidRangeException("Invalid range");
    } catch(const InvalidRangeException& exception) {
        std::cerr << exception.what() << std::endl;
        std::cout<< "Handled Invalid exception gracefully" <<std::endl;
    }
#endif

return EXIT_SUCCESS;

}

The screenshot of the code working with all the macros marked as FALSE is present below. In this case the all the exception thrown by methods are handled gracefully.

Screenshot of the code to understand the indentation is present below.


Related Solutions

Translate the following tasks into Hack C-Instructions: 1) Set D to A - 1 2) Set...
Translate the following tasks into Hack C-Instructions: 1) Set D to A - 1 2) Set both A and D to A + 1 3) Set D to 19 4) Set both A and D to A + D 5) Set RAM[5034] to D - 1 6) Set RAM[543] to 171 7) Add 1 to RAM[7], and store result in D 8) Add 3 to RAM[12], and store result in D
(In C)Set hasDigit to true if the 3-character passCode contains a digit.
  Set hasDigit to true if the 3-character passCode contains a digit. (in C) #include <stdio.h> #include <string.h> #include <stdbool.h> #include <ctype.h>   int main(void) { bool hasDigit; char passCode[50]; hasDigit = false; scanf("%s", passCode); /* Your solution goes here */ if (hasDigit) { printf("Has a digit.\n"); } else { printf("Has no digit.\n"); } return 0; }
(C++)Set hasDigit to true if the 3-character passCode contains a digit.
C++. Set hasDigit to true if the 3-character passCode contains a digit. #include #include #include using namespace std; int main() {bool hasDigit;string passCode; hasDigit = false;cin >> passCode; /* Your solution goes here */ if (hasDigit) {cout
Consider the relation R= {A, B, C, D, E, F, G, H} and the set of...
Consider the relation R= {A, B, C, D, E, F, G, H} and the set of functional dependencies: FD= {{B}—> {A}, {G}—> {D, H}, {C, H}—> {E}, {B, D}—> {F}, {D}—>{C}, {C}—> {G}} 1) Draw FD using the diagrammatic notation. 2) What are all candidate keys for R? 3) If delete {C}—>{G} and change {C, H}—> {E} to {C, H}—> {E, G}, what are all candidate keys for R
Suppose coke (c) and hamburgers (h) provide a consumer utility of U(c,h)= (c∗h)^1/2 (a) If coke...
Suppose coke (c) and hamburgers (h) provide a consumer utility of U(c,h)= (c∗h)^1/2 (a) If coke costs 1 TL and hamburger costs 25 TL, how should this consumer spend 100 TL that his mother gives him to maximize his utility? (b) Suppose government wants to discourage coke consumption by taxing coke by 3 TL. From the point of consumer is it better to tax coke or to tax income?
How many proper subsets are there for this set {A,B,C,D,E,F,G,H,I}?
How many proper subsets are there for this set {A,B,C,D,E,F,G,H,I}?
Verify that the following are true in the basic Solow growth model with no technological progress...
Verify that the following are true in the basic Solow growth model with no technological progress and no population growth: (a) The real rate of return R/P is constant along the BGP (b) The real wage W/P is constant along the BGP (c) The capital to labor ratio K/N is constant along the BGP (d) The capital to output ratio K=/Y is constant along the BGP How do these results match up to the Kaldor and Kuznets stylized facts?
Verify that the Divergence Theorem is true for the vector field F on the region E....
Verify that the Divergence Theorem is true for the vector field F on the region E. Give the flux. F(x, y, z) = xyi + yzj + zxk, E is the solid cylinder x2 + y2 ≤ 144, 0 ≤ z ≤ 4.
C++ problem 1) Describe briefly what exceptions are and what their purpose is. What is the...
C++ problem 1) Describe briefly what exceptions are and what their purpose is. What is the try block? What is the catch block? What happens with the flow of control if the catch block does not catch a thrown exception? Give a simple example. 2) Describe briefly what templates are and what their purpose is.
Make a small example that shows that a particular set of tasks can be scheduled by...
Make a small example that shows that a particular set of tasks can be scheduled by the ad hoc method and NOT by DM. short answer please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT