catching Exceptions

Hello
I’m not very familiar with writing object oriented programs so my
questions may be stupid. My job is to write a program This program reads
the date in the German date format (day, month, year). The Program
should use a class Datum (see code Datum.h). This class gots a
constructor with the day, month and year as the constructors arguments.
In this constructor i check wether the date is valid. If the date is not
valid the constructor should throw an exception from the type Datum (see
in the code). The catching object should be an instance of another class
Falsches_Datum. The code i have written up to now does not catch the
thrown exception object. I think the problem is that the thrown object
has arguments and the catching object has no arguments. Am i right or
how is it possible to catch the exception with changes in the header
files but no changes in the cpp File.
Thank you Thorsten


#include <iostream.h>
#include "datum.h"

// Testprogramm für Aufgabe 5
// Bestimmt den Folgetag zu einem korrekten Datum

void main()
{
int t,m,j;
cout << "Gib Tag: (1 .. 31)";
if(!(cin >> t))
 cout << "Unzulässige Eingabe ! " << endl;
else
{
// Rest der Zeile überlesen:
{char c; do{cin.get(c);} while(c != '\n');}
cout << "Gib Monat: (1 .. 12)";
if (!(cin >> m))
cout << "Unzulässige Eingabe ! " << endl;
else
{
// Rest der Zeile überlesen:
{char c; do{cin.get(c);} while(c != '\n');}
cout << "Gib Jahr: ( > 1581)";
if(!(cin >> j))
cout << "Unzulässige Eingabe ! " << endl;
else
try
{
 Datum heute(t,m,j);
 Datum morgen = heute.Folgetag();
 cout << "heutiges Datum : "
      << heute.Wert_von_Tag() << '.'
      << heute.Wert_von_Monat() << '.'
      << heute.Wert_von_Jahr()
      << endl;
 cout << "Folgetag : "
      << morgen.Wert_von_Tag() << '.'
      << morgen.Wert_von_Monat() << '.'
      << morgen.Wert_von_Jahr()
      << endl;
} // Ende try
catch(Falsches_Datum fd)
{
 cout  << "Datum falsch: "
       << fd.Wert_von_Tag() << '.'
       << fd.Wert_von_Monat() << '.'
       << fd.Wert_von_Jahr()
       << endl;
}
} // Ende Jahr ok
} // Ende Monat ok
} // Ende main


// M. Schoen; T. Liese
// Aufgabe 5
// Deklaration der Klasse Datum
#include "f_datum.h"
class Datum
{
// ohne public defaultmaessig private
public:
int tag,monat,jahr;
// Deklartion der Methoden (private)
bool korrekt();

// Deklaration der Methoden (public)
public:
Datum Folgetag();
int tag_von_datum();
int monat_von_datum();
int jahr_von_datum();
// Deklaration des Konstruktors
Datum(int a,int b, int c);
};

// Ende Klassendeklarationen**********************************

// Implementation der Komponenten der Klasse Datun

// Konstruktor von Datum
//**********************************************************
Datum::Datum(int a,int b,int c)
{
tag=a;
monat=b;
jahr=c;
if (korrekt()==0) throw Datum(tag,monat,jahr);
}

int Datum::tag_von_datum()
{
 return tag;
}
//**********************************************************

// Methode monat_von_datum()
//**********************************************************
int Datum::monat_von_datum()
{
 return monat;
}
//**********************************************************

// Methode jahr_von_datum()
//**********************************************************
int Datum::jahr_von_datum()
{
 return jahr;
}
//**********************************************************

// Methode korrekt
//**********************************************************
bool Datum::korrekt()
{
bool richtig;

richtig=1;
switch (monat)
{
 case 2:
 if (tag>=30)
 {
 richtig=0;
 }

 if (tag==29)
 {
    // Ist es ein Schaltjahr
        if ((jahr % 4 == 00)&&((jahr % 100 == 0)<= (jahr % 400 ==0)))
        {
        richtig=1;
        }
 else
        {
        richtig=0;
        }
 }
  break;
 case 4:
 case 6:
 case 9:
 case 11:
 if (tag>30)
 {
 richtig=0;
 }
 }
  return richtig;
}







//*****************************************************************


// Methode folgetag()
//*****************************************************************
Datum Datum::Folgetag()
{
  int t,m,j;
switch(monat)

 {

 case 4: // Monate mit 30 Tagen:
 case 6:
 case 9:
 case 11:
    if (tag == 30)
                {
                t=1;
                m=monat+1;
                j=jahr;

                }
        else
                {
                t=tag+1;
                m=monat;
                j=jahr;

                }
      break;


 case 3: // Monate mit 31 Tagen
 case 1:
 case 5:
 case 7:
 case 8:
 case 10:
        if (tag == 31)
                {
                t=1;
                m=monat+1;
                j=jahr;

                }
                else
                {
                t=tag+1;
                m=monat;
                j=jahr;

                }
       break;
case 12:
       if (tag == 31)
                {
                t=1;
                m=1;
                j=jahr+1;

                }
                else
                {
                t=tag+1;
                m=monat;
                j=jahr;

                }
       break;
case 2:
if (tag==29)
        {
        t=1;
        m=3;
        j=jahr;
        break;
        }
if (tag==28)
        {
        if ((tag %4 ==0)&&((jahr % 100 == 0)<=(jahr % 400 ==0)))
                {
      t=29;
      m=2;
      j=jahr;
      }
      else
      {
      t=1;
      m=3;
      j=jahr;
      }
      }
      if ((tag!=28)&&(tag!=29))
      {
      t=tag+1;
      m=2;
      j=jahr;
       }
            }
return Datum(t,m,j);

}

//***********************************************************





// Deklaration der Klasse Falsches_Datum
class Falsches_Datum
{
// ohne public defaultmaessig private
int tag,monat,jahr;
public:
// Deklaration der Methoden (public)
int tag_von_datum();
int monat_von_datum();
int jahr_von_datum();

// Deklaration des Konstruktors
Falsches_Datum(int a, int b, int c);
};
// Implementation der Methoden und des Konstruktors von Falsches_Datum
Falsches_Datum::Falsches_Datum(int a, int b, int c)
{
tag=a;
monat=b;
jahr=c;

}

int Falsches_Datum::tag_von_datum()
{
 return tag;
}
//**********************************************************

// Methode monat_von_datum()
//**********************************************************
int Falsches_Datum::monat_von_datum()
{
 return monat;
}
//**********************************************************

// Methode jahr_von_datum()
//**********************************************************
int Falsches_Datum::jahr_von_datum()
{
 return jahr;
}
//**********************************************************
thorstenAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
nietodConnect With a Mentor Commented:
You are throwing the wrong type.

contineus.
0
 
nietodCommented:
You are expecting to catch a Falsches_Datum (false data?) type in the line

catch(Falsches_Datum fd)

but in the constructor, you throw a Datum type,  not a Falsches_Datum type in

if (korrekt()==0) throw Datum(tag,monat,jahr);

change this line to

if (korrekt()==0) throw Falshes_Datum(tag,monat,jahr);

and you should be okay.  Let me know if you have any questions.
0
 
alexoCommented:
Catching by reference (rather than by value) is preferred.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
nietodCommented:
True, not that its the problem, through.  To catch by reference you would do

catch(Falsches_Datum &fd)

This is more efficient than catching by value and also works better when you are catching objects that are derived from a base class that you catch with.
0
 
thorstenAuthor Commented:
You are right.
But one problem left. If i throw the Exception from the type Falsches_Datum in the constructor of Datum the class Falsches_Datum is not known in the class Datum. I solve this problem using the friend statement. Are there any other possibilities to get access to the constructor of Falsches_datum out of the class Datum
Thanks  
0
 
nietodCommented:
From what I see at the moment, I don't see any reason why Falsches_Datum needs to be a friend of Datum.  It looks to me like you just need to declare Falsches_Datum before Datum and then you should be fine.  (i.e Falsches_Datum doesn't depend on Datum, so declare it first..)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.