I wrote a small program but I am having problems getting my try catch block to work. Any help would be appreciated.
This is the exception file:
1 2 3 4 5 6 7 8 9 10 11 12
#include <stdexcept>
usingnamespace std;
class EmployeeIdNotInteger : public logic_error
{
public:
EmployeeIdNotInteger()
: logic_error("Empoyee number can only be a integer"){}
};
This is because you aren't throwing the exception, so the catch block never catchs it.
But there's something strange... empId is always an integer (because the function argument is a int type), so I think the exception will never raises.
You shold write the try block when you call setEmpId.
Like:
thanks for the suggestion. I tried adding the try block to the employee constructor and still does not catch the error. I tried passing a double (10.2) and it returned 10. Then I tried passing a string and it returned the memory location.
I am not sure how to answer the question but those variables are suppose to be integers. They are defined to be int.
Here is the definition of the exception class:
1 2 3 4 5 6 7 8 9 10 11 12
#include <stdexcept>
usingnamespace std;
class EmployeeIdNotInteger : public logic_error
{
public:
EmployeeIdNotInteger()
: logic_error("Empoyee number can only be a integer"){}
};
#include <xtr1common>
template <typename id_type = int>
class EmployeeIdNotInteger : public logic_error
{
static_assert(!std::tr1::is_integral<id_type>::value,
"Empoyee number can only be a integer");
EmployeeIdNotInteger();
};
I have not covered templates yet so I don't know how to implement the template and this is homework; therefore, I should use the conditions given.
The condition for this homework was pretty simple. It was to create a program and show how exceptions are implemented in constructors and how destructors destroy everything that was built before the exception was caught.
I am hoping I can work something out with this Employee class I used before and implement a try block for it.
I decided to add the try block directly into the constructor instead of using it in the setter. Before I made this changes, if I used the wrong parameters, the program would just return the wrong parameter as the employee number. I can't get it to return the string.
You could insist that employee IDs meet some totally arbitrary requirement, so you can then break the rule in your test. i.e. it must always be of the form 8nnn (n = 0..9), or must be even. I take it that any rule will do, as long as it triggers an exception? (If it has to be 4 digits long and begin with an 8, that gives you two possible errors!)
And I would throw one of the std::exceptions, or a derived class, as you're doing above, rather than a string or int.
I was hoping that just by using a string instead of an integer it would throw an exception. I moved the try block directly into the constructor. This is the new constructor:
I am hoping that once I catch the exception, the destructors would be called on the rest of the constructor. However, I am still unable to get it to fail. If it fails, it just returns the location number. My header is the same as above.
Does your setEmpId still take an int? Passing a double to this function will just cause the compiler to truncate the number to an int automatically. Possibly with a cmpiler warning, but not a lot else.
If you modify the function do take a double, then you can use test it to see that the double passes has no decimal part.
I am entering strings, doubles, and anything else that is not an integer but nothing passing to the catch block. I just did not post my testfile here. I am playing with your code right now and see what I can get out of it.
yes, setEmpId takes an int. I figure that if I try to pass a double, the compiler was going to truncate the decimal. I am using a string to cause the fail. Here is what I am using as my test file:
It looks like it's calling set using the existing member variables. For the string members this is pointless: they will correctly zero themselves in their own constructors. For the ints it is setting them back to whatever random value they hold. It might be better to start off by zero-ing them? Similarly for the double.
1 2 3 4
Employee::Employee() : empId(0), age(0), salary(0.0)
{
// name and last look after themselves
}
Ideally you would initialize (prob to zero) all non-class member variables in the constructor initializer list.
Thanks for the advice. I added the "Return 0;" to main().
I changed my constructor to include the try block for the empId. I was thinking that it would be easier to test for the value inside the constructor instead of the setter.