http://gcc.gnu.org/ml/gcc-prs/2000-q3/msg00389.html
καλό ε ?
Δεν είναι 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);
>
>
>
>
>
>
>
> }
Αν και δεν ήξερα ότι είναι 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:
--
TheHackerspace - Thessaloniki's Hackerspace
http://the-hackerspace.org/
μάλιστα...