[Fwd: constructors destructors exception handling]

8 views
Skip to first unread message

Demosthenes Koptsis

unread,
Jan 7, 2012, 5:45:47 AM1/7/12
to TheHackerspace
Λοιπόν βρήκα ότι έχει bug ο gcc και δεν κάνει catch το division by zero
και floating point exceptions.

http://gcc.gnu.org/ml/gcc-prs/2000-q3/msg00389.html

καλό ε ?

Message has been deleted

TheHackerspace

unread,
Jan 7, 2012, 9:43:45 AM1/7/12
to TheHackerspace
Δήμο αυτό το exception είναι hardware και το πετάει η CPU. H standard C
++ δεν το περιγράφει.

Δεν είναι bug του GCC. Θα το κάνεις έτσι:

#include <stdexcept>
#include <iostream>

using namespace std;

class TestClass
{
public:
static double divide (const int a, const int b);
};

double TestClass::divide (const int a, const int b)
{
if (b == 0)
{
throw invalid_argument ("Divide By Zero!");
}

return (a / b);
}

int main (int argc, char *argv[])
{
try
{
double result = TestClass::divide (10, 0);

cout << "Division: " << result << endl;
}
catch (exception & e)
{
cout << "Exception: " << e.what () << endl;
}

return 0;
}

----

On Jan 7, 12:45 pm, Demosthenes Koptsis <demosthen...@gmail.com>
wrote:

> [ Attached Message ]From:Demosthenes Koptsis <demosthen...@gmail.com>To:TheHackerspace <thessaloniki...@googlegroups.com>Date:Fri, 06 Jan 2012 12:58:33 +0200Local:Fri, Jan 6 2012 12:58 pmSubject:constructors destructors exception handlingΚαλημέρα,
>
> ψάχνω να βρω ένα παράδειγμα για να διαχειρίζομαι 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);
>
>
>
>
>
>
>
> }

Demosthenes Koptsis

unread,
Jan 7, 2012, 1:18:14 PM1/7/12
to thessaloniki...@googlegroups.com
Α οκ! Και γιατί hardware exception?


Αν και δεν ήξερα ότι είναι hardware ex. βρήκα ότι πρέπει να το κάνω
throw.


On Sat, 2012-01-07 at 06:41 -0800, TheHackerspace wrote:
> Δήμο το "Divide By Zero" είναι hardware exception και το πετάει η CPU
> και δεν είναι software exception.
>
> "Integer divide by zero is not an exception in standard C++."
>
> Αυτό δεν είναι bug του GCC.
>
> Θα πρέπει να το χειριστείς εσύ και να το κάνεις throw.
>
> Έτσι δηλαδή:


>
> include <stdexcept>
> #include <iostream>
>
> using namespace std;
>
> class TestClass
> {
> public:
> static double divide (const int a, const int b);
> };
>
> double TestClass::divide (const int a, const int b)
> {
> if (b == 0)
> {
> throw invalid_argument ("Divide By Zero!");
> }
>
> return (a / b);
> }
>
> int main (int argc, char *argv[])
> {
> try
> {
> double result = TestClass::divide (10, 0);
>
> cout << "Division: " << result << endl;
> }
> catch (exception & e)
> {
> cout << "Exception: " << e.what () << endl;
> }
>
> return 0;
> }
>

> On Jan 7, 12:45 pm, Demosthenes Koptsis <demosthen...@gmail.com>
> wrote:

Stavros Kalapothas

unread,
Jan 7, 2012, 2:00:59 PM1/7/12
to thessaloniki...@googlegroups.com
τα hw exceptions είναι τα πιο low level (divide by 0, seg. fault, mem_access violations, overflows) αυτά δλδ που προκύπτουν από τις αρχιτεκτονικές των επεξεργαστών

τα sw exceptions προκύπτουν από τις αρχιτεκτονικές των compiler και os που γράφεις

Σταύρος,



--
TheHackerspace - Thessaloniki's Hackerspace

http://the-hackerspace.org/

Demosthenes Koptsis

unread,
Jan 8, 2012, 2:49:28 AM1/8/12
to thessaloniki...@googlegroups.com
Καλημέρα,

μάλιστα...

Reply all
Reply to author
Forward
0 new messages