constructors destructors exception handling

2 views
Skip to first unread message

Demosthenes Koptsis

unread,
Jan 6, 2012, 5:58:33 AM1/6/12
to TheHackerspace
Καλημέρα,

ψάχνω να βρω ένα παράδειγμα για να διαχειρίζομαι errors στους
constructors.

Έκανα το παρακάτω παράδειγμα αλλά κάτι δεν κάνω καλά και δεν λειτουργεί.

Στο παράδειγμα αυτό προσπαθώ να πιάσω το error στον constructor αλλά δεν
μπορώ.


Πώς γίνεται;


Test.h
-----------
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

class Test
{
public:
Test(int, int);
~Test();
private:
double result;
};

#endif // TEST_H_INCLUDED


---------------------------------

Test.cpp
----------
#include <iostream>
#include "Test.h"
using namespace std;

Test::Test(int num1, int num2)
{
cout << "In constructor divide by zero" << endl;

try
{
result = num1/num2; //create the error
}
catch (exception& e)
{
cerr << "exception caught: " << e.what() << endl;
}

}

Test::~Test()
{
cout << "In destructor throw exception handling" << endl;

}


-----------------------------------

main.cpp
---------
#include <iostream>
#include "Test.h"

using namespace std;

int main()
{ int number1;
int number2;

number1=1;
number2=0;

Test object1(number1, number2);
}


Reply all
Reply to author
Forward
0 new messages