Unix True 64 - c++ exception handling

This is Interesting: Free IT Magazines  
Home > Archive > Unix True 64 > February 2004 > c++ exception handling





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author c++ exception handling
Scott Schindler

2004-02-06, 8:35 am

I am trying to test an exception handling scheme in c++ on a unix machine
(uname -a produces the following output: OSF1 *removed* V5.1 1885 alpha
alpha)

Here is the code:

#include <exception>
#include <iostream>

int main()
{
try
{
int numerator(100);
int denominator(0);
// Should cause an exception to occur.
int result(numerator/denominator);
}
catch (...)
{
std::cout << "Exception caught by catch all.\n";
}

return 0;
}

compiling with the following command:
cxx -std strict_ansi -std strict_ansi_errors -g -O0 test.cpp

running with the following command:
a.out

produces the following output:
Floating exception


I have also tried the following example:

#include <exception>
#include <iostream>
#include <signal.h>

extern "C"
{
void handler(int i)
{
std::cout << "Exception caught by the signal handler.\n";
throw 1;
}
}

int main()
{
sginal(SIGFPE, handler);

try
{
int numerator(100);
int denominator(0);
// Should cause an exception to occur.
int result(numerator/denominator);
}
catch (...)
{
std::cout << "Exception caught by catch all.\n";
}

return 0;
}

Using the same compilation and running commands produces the following
output:
Exception caught by the signal handler.
Exception caught by catch all.



I am not sure that I want to implement the second method. Is there a
pure c++ way of having the program catch the exception.

Thank you for your time,

Scott Schindler
Steffen Neumann

2004-02-08, 9:34 pm

Scott Schindler <notcorrect@whocares.com> writes:

> I am trying to test an exception handling scheme in c++ on a unix machine
> (uname -a produces the following output: OSF1 *removed* V5.1 1885 alpha
> alpha)

[...]
> produces the following output:
> Floating exception

[...]
> Using the same compilation and running commands produces the following
> output:
> Exception caught by the signal handler.
> Exception caught by catch all.


> I am not sure that I want to implement the second method. Is there a
> pure c++ way of having the program catch the exception.


Well, "int" is not an object data type,
therefore there is nobody "throwing" the exception.

Either you look for some C++ math library that
wraps all Integer into C++ objects with exception handling,
or you go it your way.

Yours,
Steffen

Scott Schindler

2004-02-09, 12:34 am

Steffen Neumann <sneumann@TechFak.Uni-Bielefeld.DE> wrote in
news:s5ubro88qhl.fsf@liszt.TechFak.Uni-Bielefeld.DE:

> Scott Schindler <notcorrect@whocares.com> writes:
>
> [...]
> [...]
>
>
> Well, "int" is not an object data type,
> therefore there is nobody "throwing" the exception.
>
> Either you look for some C++ math library that
> wraps all Integer into C++ objects with exception handling,
> or you go it your way.
>
> Yours,
> Steffen
>
>


I am not sure I understand what your saying by "int" is ont an object data
type. Are you saying that the throw in the handler is invalid because it
is throwing an int? My concern is that I cannot catch a floating point
exception on a tru64 machine in c++.

Thank you for your time,
Scott
Falk Hueffner

2004-02-09, 12:34 am

Scott Schindler <notcorrect@whocares.com> writes:

> I am trying to test an exception handling scheme in c++ on a unix machine
> (uname -a produces the following output: OSF1 *removed* V5.1 1885 alpha
> alpha)
>
> int numerator(100);
> int denominator(0);
> // Should cause an exception to occur.


The C++ language says behaviour of division by zero is undefined; some
environments produce an exception for it, but you cannot rely on that
for portable programs.

> I am not sure that I want to implement the second method. Is there
> a pure c++ way of having the program catch the exception.


No.

--
Falk
Scott Schindler

2004-02-09, 1:35 am

Falk Hueffner <falk.hueffner@student.uni-tuebingen.de> wrote in
news:87bro8e6bb.fsf@student.uni-tuebingen.de:

> Scott Schindler <notcorrect@whocares.com> writes:
>
>
> The C++ language says behaviour of division by zero is undefined; some
> environments produce an exception for it, but you cannot rely on that
> for portable programs.
>
>
> No.
>


Thank you,

Scott
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com