Solved

newbie question on exceptions, easy money

Posted on 2002-05-23
4
205 Views
Last Modified: 2010-04-02
I'm new to exceptions, here's the background:
1 singleton
1 customer header and source file
1 employee header and source file
1 baseops header and source (creates id's)
1 class customer list that contians a map to link name and id.
1 for employee as well

would like to have customer id begin with 1 and employee begin with 2.  
on the menu you can add/delete/modify a customer or employee.. I want it to be if the user selects add employee and starts to enter an e.id=1 an error will be thrown (invalid id for employee, or something).

Question:  how should my header file and source file look for employee Error?  Do I need to add anything to my baseops?

portion of code:  be kind, I'm new!

//customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H

#include "BaseOPS.h"
#include "BaseOPSFactory.h"
class BaseOPSFactory;

class Customer : public BaseOPS{
friend class BaseOPSFactory;
public:
     void getData(); //throw (CustomerListError);
     void setData(string); //throw (CustomerListError);
     void modifyData(); //throw (CustomerListError);
     ~Customer();
     void showData();//throw ();

protected:
     Customer();    
private:
     string Name;
     
};
#endif


//Customer.cpp

#include "Customer.h"
//#include "Credit.h"

// #include "CustomerListError.h"
#include <iostream>

Customer::Customer():BaseOPS()
{
}

void Customer::getData() //throw (CustomerListError)
{
     cout << "Enter customer name:      " <<endl;
     cin >> Name;      
     cout << endl;

     /*
     if(Name >       // if name is not a char then throw error  (if name!= char)
     {
          throw Error;
     }*/
}



void Customer::modifyData() //throw (CustomerListError)
{
     cout << "Customer name:         " << Name;      
     cout << endl;
     cout << "Enter customer name:     " <<endl;
     cin >> Name;      
     cout << endl;

}

Customer::~Customer()
{
     
}

void Customer::setData(string D)
{
     BaseOPS::setData(D);
}
void Customer::showData()
{
     BaseOPS::showData();
     cout << "Customer name is:          " << Name ;

}

0
Comment
Question by:student1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
ct.smith earned 50 total points
ID: 7029951
C++ allows you to use anything for an exception (even primitive types like int and float), so the thing to do is to decide what information is relevant to the error condition and wrap it up nicely in some sort of class/struct/enum ...

A few nice (completely non-obligatory) conventions:
- use a class
- append 'Exc' to your class name
- inerit from the STL exception class.
- don't use the word 'error' when referring to exceptions, there's a difference that some people get rather caught up about.

example header file:

#ifndef __IDEXC_H__
#define __IDEXC_H__

#inlcude <exception>

class IdExc : public std::exception {
public:
  const char* what() {return "Invalid Employee ID";}
};

#endif

Of course, you will want to expand this template to whatever information is useful to your application.  But,
at least this code segment should get you started.
0
 

Author Comment

by:student1
ID: 7030669
thanks for pointing me in the right direction... I should be able to create a template class and adapt it to all necessary classes, ie. employee, customer, vendor, product.. correct?
0
 
LVL 4

Expert Comment

by:ct.smith
ID: 7030723
Yes, in fact if you have several related types, inheriting something like my example would make a nice way to organise the various exceptions.

Good luck!
0
 

Author Comment

by:student1
ID: 7032367
CT.. Because all my classes, ie. employee/customer/vendor/product, are basically the same, ie. get name/set name/modify name/etc... can I just create 1 template class like my exception template class?

anthony
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question