Solved

catching Exceptions

Posted on 1999-01-17
6
357 Views
Last Modified: 2010-04-02
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;
}
//**********************************************************
0
Comment
Question by:thorsten
  • 4
6 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 300 total points
ID: 1184406
You are throwing the wrong type.

contineus.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1184407
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
 
LVL 11

Expert Comment

by:alexo
ID: 1184408
Catching by reference (rather than by value) is preferred.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1184409
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
 

Author Comment

by:thorsten
ID: 1184410
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
 
LVL 22

Expert Comment

by:nietod
ID: 1184411
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now