nested try/catch block and _set_se_translator 
Author Message
 nested try/catch block and _set_se_translator

Maybe you can rethrow by "throw e;" instead of "throw;". That would
essentially drop the current exception propagation and start a new one,
this time pure C++.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> Hi, everybody,

> We're using _set_se_translator to convert SE to C++ exceptions and one
> problem we're having is that the translator function will be called
multiple
> times if we have nested try/catch blocks and we re-throw the excetion
inside
> the try/catch chain. See the code below.

> I know this is by design and it's bad trying to recover from
exceptions like
> access violations. But I'm still wondering if there is anyway to
change this
> behavior so the translater function will only be called once for each
SE.

> Thanks,

> Sean

> // se.cpp : Defines the entry point for the console application.
> //
> #include <iostream>
> #include <Windows.h>
> #include <eh.h>

> void SEFunc();
> void func1();
> void trans_func( unsigned int, EXCEPTION_POINTERS* );

> class SE_Exception
> {
> private:
>     unsigned int nSE;
> public:
>     SE_Exception() {}
>     SE_Exception( unsigned int n ) : nSE( n ) {}
>     ~SE_Exception() {}
>     unsigned int getSeNumber() { return nSE; }
> };

> void main( void )
> {
>     _set_se_translator( trans_func );

>     try {
>         func1();
>     }
>     catch (SE_Exception& e) {
>         std::cerr<<"Caught a __try exception with SE_Exception."
> <<std::endl;
>     }

>     return;
> }
> void func1()
> {
>     try {
>         SEFunc();
>     }
>     catch( SE_Exception& e ) {
>         std::cerr<<"Caught a __try exception with SE_Exception."
> <<std::endl;
>         throw ;
>     }
> }
> void SEFunc()
> {
>         int *p = (int*) 0;
>         *p = 1;
> }
> void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
> {
>     std::cout<<"In trans_func."<<std::endl;
>     throw SE_Exception();
> }
> Output:

> In trans_func.
> Caught a __try exception with SE_Exception.
> In trans_func.
> Caught a __try exception with SE_Exception.



Tue, 07 Dec 2004 01:32:12 GMT  
 nested try/catch block and _set_se_translator
Hi, everybody,

We're using _set_se_translator to convert SE to C++ exceptions and one
problem we're having is that the translator function will be called multiple
times if we have nested try/catch blocks and we re-throw the excetion inside
the try/catch chain. See the code below.

I know this is by design and it's bad trying to recover from exceptions like
access violations. But I'm still wondering if there is anyway to change this
behavior so the translater function will only be called once for each SE.

Thanks,

Sean

// se.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <Windows.h>
#include <eh.h>

void SEFunc();
void func1();
void trans_func( unsigned int, EXCEPTION_POINTERS* );

class SE_Exception
{
private:
    unsigned int nSE;
public:
    SE_Exception() {}
    SE_Exception( unsigned int n ) : nSE( n ) {}
    ~SE_Exception() {}
    unsigned int getSeNumber() { return nSE; }

Quote:
};

void main( void )
{
    _set_se_translator( trans_func );

    try {
        func1();
    }
    catch (SE_Exception& e) {
        std::cerr<<"Caught a __try exception with SE_Exception."
<<std::endl;
    }

    return;

Quote:
}

void func1()
{
    try {
        SEFunc();
    }
    catch( SE_Exception& e ) {
        std::cerr<<"Caught a __try exception with SE_Exception."
<<std::endl;
        throw ;
    }
Quote:
}

void SEFunc()
{
        int *p = (int*) 0;
        *p = 1;
Quote:
}

void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
    std::cout<<"In trans_func."<<std::endl;
    throw SE_Exception();
Quote:
}

Output:

In trans_func.
Caught a __try exception with SE_Exception.
In trans_func.
Caught a __try exception with SE_Exception.



Tue, 07 Dec 2004 01:25:11 GMT  
 nested try/catch block and _set_se_translator
Igor,

Thanks for your suggestion.  That solved the problem. Here is another one
and actually is the one we first noticed. When the second access violation
happens, the translator function will be called twice!

Again, thanks for you help.

Sean

// se.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <Windows.h>
#include <eh.h>

void SEFunc();
void func1();
void func2();
void trans_func( unsigned int, EXCEPTION_POINTERS* );

class SE_Exception
{
private:
    unsigned int nSE;
public:
    SE_Exception() {}
    SE_Exception( unsigned int n ) : nSE( n ) {}
    ~SE_Exception() {}
    unsigned int getSeNumber() { return nSE; }

Quote:
};

void main( void )
{
    _set_se_translator( trans_func );

    try {
        func1();
    }
    catch (SE_Exception& e) {
        std::cerr<<"Caught a __try exception with SE_Exception."
<<std::endl;
    }

    try {
        func2();
    } catch (SE_Exception& e) {
        std::cerr<<"Caught a __try exception with SE_Exception."
<<std::endl;
    }

    return;

Quote:
}

void func1()
{
    try {
        SEFunc();
    }
    catch( SE_Exception& e ) {
        std::cerr<<"Caught a __try exception with SE_Exception."
<<std::endl;
        throw e;
    }

Quote:
}

void func2()
{
    try {
        func1();
    }
    catch( SE_Exception& e ) {
        std::cerr<<"Caught a __try exception with SE_Exception."
<<std::endl;
    }

    SEFunc();

Quote:
}

void SEFunc()
{
        int *p = (int*) 0;
        *p = 1;
Quote:
}

void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
    std::cout<<"In trans_func."<<std::endl;
    throw SE_Exception();

Quote:
}

Output:

In trans_func.
Caught a __try exception with SE_Exception.
Caught a __try exception with SE_Exception.
In trans_func.
Caught a __try exception with SE_Exception.
Caught a __try exception with SE_Exception.
In trans_func.
In trans_func.
Caught a __try exception with SE_Exception.



Tue, 07 Dec 2004 01:48:52 GMT  
 nested try/catch block and _set_se_translator
From the docs on _set_se_translator:

<quote>
The translator function that you write is called once for each function
invocation on the stack that has try blocks.
</quote>

So, when you call SEFunc from f2 for the second time, you have two
functions above SEFunc on the stack - f2 and main - each with try
blocks. The behavior is by design.

Why are you so worried about translator being called more than once?
Efficiency is out the window by that time anyway, the program just
struggles to survive.
--
With best wishes,
    Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken


Quote:
> Igor,

> Thanks for your suggestion.  That solved the problem. Here is another
one
> and actually is the one we first noticed. When the second access
violation
> happens, the translator function will be called twice!

> Again, thanks for you help.

> Sean

> // se.cpp : Defines the entry point for the console application.
> //
> #include <iostream>
> #include <Windows.h>
> #include <eh.h>

> void SEFunc();
> void func1();
> void func2();
> void trans_func( unsigned int, EXCEPTION_POINTERS* );

> class SE_Exception
> {
> private:
>     unsigned int nSE;
> public:
>     SE_Exception() {}
>     SE_Exception( unsigned int n ) : nSE( n ) {}
>     ~SE_Exception() {}
>     unsigned int getSeNumber() { return nSE; }
> };

> void main( void )
> {
>     _set_se_translator( trans_func );

>     try {
>         func1();
>     }
>     catch (SE_Exception& e) {
>         std::cerr<<"Caught a __try exception with SE_Exception."
> <<std::endl;
>     }

>     try {
>         func2();
>     } catch (SE_Exception& e) {
>         std::cerr<<"Caught a __try exception with SE_Exception."
> <<std::endl;
>     }

>     return;
> }
> void func1()
> {
>     try {
>         SEFunc();
>     }
>     catch( SE_Exception& e ) {
>         std::cerr<<"Caught a __try exception with SE_Exception."
> <<std::endl;
>         throw e;
>     }
> }

> void func2()
> {
>     try {
>         func1();
>     }
>     catch( SE_Exception& e ) {
>         std::cerr<<"Caught a __try exception with SE_Exception."
> <<std::endl;
>     }

>     SEFunc();
> }

> void SEFunc()
> {
>         int *p = (int*) 0;
>         *p = 1;
> }
> void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
> {
>     std::cout<<"In trans_func."<<std::endl;
>     throw SE_Exception();
> }

> Output:

> In trans_func.
> Caught a __try exception with SE_Exception.
> Caught a __try exception with SE_Exception.
> In trans_func.
> Caught a __try exception with SE_Exception.
> Caught a __try exception with SE_Exception.
> In trans_func.
> In trans_func.
> Caught a __try exception with SE_Exception.



Tue, 07 Dec 2004 02:25:39 GMT  
 nested try/catch block and _set_se_translator
Igor,

Thanks for the quick responce. We knew it was by design but was wondering if
there was anyway to change the behavior (such as a function all).  We're not
worrying about the efficiency. The problem we're havning right now is
because we're dumping call stack and minidumps in the translator function.
On windows XP, we can use vectored exception handling but our application
still needs W2K support.

Best,

Sean


Quote:
> From the docs on _set_se_translator:

> <quote>
> The translator function that you write is called once for each function
> invocation on the stack that has try blocks.
> </quote>

> So, when you call SEFunc from f2 for the second time, you have two
> functions above SEFunc on the stack - f2 and main - each with try
> blocks. The behavior is by design.

> Why are you so worried about translator being called more than once?
> Efficiency is out the window by that time anyway, the program just
> struggles to survive.
> --
> With best wishes,
>     Igor Tandetnik

> "For every complex problem, there is a solution that is simple, neat,
> and wrong." H.L. Mencken



> > Igor,

> > Thanks for your suggestion.  That solved the problem. Here is another
> one
> > and actually is the one we first noticed. When the second access
> violation
> > happens, the translator function will be called twice!

> > Again, thanks for you help.

> > Sean

> > // se.cpp : Defines the entry point for the console application.
> > //
> > #include <iostream>
> > #include <Windows.h>
> > #include <eh.h>

> > void SEFunc();
> > void func1();
> > void func2();
> > void trans_func( unsigned int, EXCEPTION_POINTERS* );

> > class SE_Exception
> > {
> > private:
> >     unsigned int nSE;
> > public:
> >     SE_Exception() {}
> >     SE_Exception( unsigned int n ) : nSE( n ) {}
> >     ~SE_Exception() {}
> >     unsigned int getSeNumber() { return nSE; }
> > };

> > void main( void )
> > {
> >     _set_se_translator( trans_func );

> >     try {
> >         func1();
> >     }
> >     catch (SE_Exception& e) {
> >         std::cerr<<"Caught a __try exception with SE_Exception."
> > <<std::endl;
> >     }

> >     try {
> >         func2();
> >     } catch (SE_Exception& e) {
> >         std::cerr<<"Caught a __try exception with SE_Exception."
> > <<std::endl;
> >     }

> >     return;
> > }
> > void func1()
> > {
> >     try {
> >         SEFunc();
> >     }
> >     catch( SE_Exception& e ) {
> >         std::cerr<<"Caught a __try exception with SE_Exception."
> > <<std::endl;
> >         throw e;
> >     }
> > }

> > void func2()
> > {
> >     try {
> >         func1();
> >     }
> >     catch( SE_Exception& e ) {
> >         std::cerr<<"Caught a __try exception with SE_Exception."
> > <<std::endl;
> >     }

> >     SEFunc();
> > }

> > void SEFunc()
> > {
> >         int *p = (int*) 0;
> >         *p = 1;
> > }
> > void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
> > {
> >     std::cout<<"In trans_func."<<std::endl;
> >     throw SE_Exception();
> > }

> > Output:

> > In trans_func.
> > Caught a __try exception with SE_Exception.
> > Caught a __try exception with SE_Exception.
> > In trans_func.
> > Caught a __try exception with SE_Exception.
> > Caught a __try exception with SE_Exception.
> > In trans_func.
> > In trans_func.
> > Caught a __try exception with SE_Exception.



Tue, 07 Dec 2004 02:50:11 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. can I nest try/catch blocks?

2. The if ~ else block in try~catch block

3. Nesting try, catch, finally

4. TRY-CATCH vs. try-catch

5. try/catch VS TRY/CATCH

6. Optimizer ignores try-catch block

7. try catch block

8. does atl support try..catch block?(empty inside)

9. Overhead of try-catch blocks?

10. /Og causes intermittent omission of try..catch block

11. try{} catch{} block and Global Optimization

12. Error processing using try-catch blocks

 

 
Powered by phpBB® Forum Software