I tried throwing an exception class that I made. Why does it say I have an unhandled exception?

Unhandled exception at at 0x76C24B32 in exception_project.exe: Microsoft C++ exception: ProductionWorker::InvalidShift at memory location 0x001CF8A3.

This is an exercise I got from a book... I need to modify this code so that the ProductionWorker class throws an exception called InvalidShift when it is passed something other than 1 or 2. So I made a regular empty class in ProductionWorker called InvalidShift and tried throwing it from the getShiftName function in ProductionWorker.h.

This is the Employee class that ProductionWorker inherits from... you probably don't need to read it. All of the code is correct because I haven't modified it. I only modified the source.cpp and ProductionWorker.h

Employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Specification file for the Employee class
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;

class Employee
{
private:
	string name;		// Employee name
	string number;		// Employee number
	string hireDate;	// Hire date

public:
	// Default constructor
	Employee()
		{ name = ""; number = ""; hireDate = ""; }

	// Constructor
	Employee(string aName, string aNumber, string aDate)
		{ name = aName; number = aNumber; hireDate = aDate; }

	// Mutators
	void setName(string n)
		{ name = n; }

	void setNumber(string num)
		{ number = num; }

	void setHireDate(string date)
		{ hireDate = date; }

	// Accessors
	string getName() const
		{ return name; }

	string getNumber() const
		{ return number; }

	string getHireDate() const
		{ return hireDate; }
};

#endif 





ProductionWorker.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Specification file for the ProductionWorker Class
#ifndef PRODUCTION_WORKER_H
#define PRODUCTION_WORKER_H
#include "Employee.h"
#include <string>
using namespace std;

class ProductionWorker : public Employee
{
private:
	int shift;		// The worker's shift
	double payRate;	// The worker's hourly pay rate

public:
	class InvalidShift
	{};

	// Default constructor
	ProductionWorker() : Employee()
		{ shift = 0; payRate = 0.0; }

	// Constructor
	ProductionWorker(string aName, string aNumber, string aDate,
		int aShift, double aPayRate) : Employee(aName, aNumber, aDate)
		{ shift = aShift; payRate = aPayRate; }

	// Mutators
	void setShift(int s)
		{ shift = s; }

	void setPayRate(double r)
		{ payRate = r; }

	// Accessors
	int getShiftNumber() const
		{ return shift; }

	string getShiftName() const
		{ if (shift == 1)
			 return "Day";
		  else if (shift == 2)
			 return "Night";
		  else
			throw InvalidShift();
		}
	double getPayRate() const
		{ return payRate; }
};

#endif 



Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Chapter 15, Programming Challenge 1: Employee and ProductionWorker classes
#include <iostream>
#include <iomanip>
#include "ProductionWorker.h"
using namespace std;

// Function prototype
void displayInfo(ProductionWorker);



int main()
{

	try
	{
		ProductionWorker pw("John Jones", "123", "1/1/2006", 3, 18.00);
	}
	catch(ProductionWorker::InvalidShift)
	{
		cout << "Invalid shift";
	}

	

	displayInfo(pw);
	cin.get();
	return 0;
}

//******************************************************
// The displayInfo function displays a production      *
// worker's employment information.                    *
//******************************************************
void displayInfo(ProductionWorker e)
{
	cout << setprecision(2) << fixed << showpoint;
	cout << "Name: " 
		 << e.getName() << endl;
	cout << "Employee number: " 
		 << e.getNumber() << endl;
	cout << "Hire date: " 
		 << e.getHireDate() << endl;
	cout << "Shift: " 
		 << e.getShiftName() << endl;
	cout << "Shift number: " 
		 << e.getShiftNumber() << endl;
	cout << "Pay rate: " 
		 << e.getPayRate() << endl;
}
Last edited on
catch takes a parameter which should be a variable of the type of exception that needs to be caught:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{

	try
	{
		ProductionWorker pw("John Jones", "123", "1/1/2006", 3, 18.00);
	}
	catch( const ProductionWorker::InvalidShift &invalid)
	{
		cout << "Invalid shift";
	}

	

	displayInfo(pw);
	cin.get();
	return 0;
}


If you wondering why the catch block catches the exception by reference-const, then http://www.informit.com/articles/article.aspx?p=373339&seqNum=2
Last edited on
Thanks.

Does anyone know why I'm getting 'pw' : undeclared identifier now? I tried Including both Employee.h and ProductionWorker.h in Source.cpp and that didn't fix it.
Because pw stopped existing before you arrived at line 26 in source.cpp.
How would you display pw if it never finished getting constructed? What would you be displaying?

Change it to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
  try
  {
    ProductionWorker pw(..........);
    diplsayInfo(pw);
  }
  catch( ... )
  {
     ...
  }
  get.get();
  return 0;
}
Topic archived. No new replies allowed.