How to detect if an exception has been thrown

I need to know if in my current thread an exception has been thrown, is there any elegante way to know this? I need to add that code to myRollbackTransaction method, I want to know if there is no transaction in the TLS cause something failed previously.
[271 byte] By [JuanRoman] at [2007-12-22]
# 1
such as the below code?
Try
' execute code
Catch ex as Exception
' Roll back the tarnsaction here
MyTransaction.RollBack();
' dispose of transaction
throw ex
Finally
' handle cleanup here perhaps

or
try
{
}
catch (Exception ex)
{
MyTarnsaction.RollBack();
throw ;
}
MarcD at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 2

That is what I have on the code... What I need now is do detect from the Rollback Transaction method wether or not an error has occurred. To be more clear, this is the scenario that I have:

BusinessComponent::DoSomethingThatRequiresTransactions()
{
DatabaseOperator.BeginTransaction(KnownDatabases.Global);

DataComponentA::DoSomething();
DataComponentB::DoSomethingElse();

DatabaseOperator.Commit();
}
catch
{
DatabaseOperator.Rollback();
throw;
}


DataComponentA::DoSomething()
{
DatabaseOperator.BeginTransaction(KnownDatabases.Global);

ExecuteProcedure();
ExecuteAnotherProcedure();

DatabaseOperator.Commit();
}
catch
{
DatabaseOperator.Rollback();
throw;
}


In this scenario if DataComponentA performs rollback transaction everything will be OK, but if BusinessComponent tries to do a Rollback again (in a nested transaction) then we have the problem of my question. How can I know that there is an error in the thread. Is there a way to know this?

JuanRoman at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 3

Can't you define this by the responsibilities/conventions of you components e.g. ensure that DataComponents do not rollback only throw allowing the business component to manage the transaction?

I take it this isn't a .Net transaction othewise you tell from the transaction object whether its been rolled back e.g.
if (transaction.TransactionInformation.Status ==
TransactionInformation.Aborted)
{
...
}

Duncan

DuncanWoods at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 4
Juan Roman wrote:

That is what I have on the code... What I need now is do detect from the Rollback Transaction method wether or not an error has occurred. To be more clear, this is the scenario that I have:

BusinessComponent::DoSomethingThatRequiresTransactions()
{
DatabaseOperator.BeginTransaction(KnownDatabases.Global);

DataComponentA::DoSomething();
DataComponentB::DoSomethingElse();

DatabaseOperator.Commit();
}
catch
{
DatabaseOperator.Rollback();
throw;
}


DataComponentA::DoSomething()
{
DatabaseOperator.BeginTransaction(KnownDatabases.Global);

ExecuteProcedure();
ExecuteAnotherProcedure();

DatabaseOperator.Commit();
}
catch
{
DatabaseOperator.Rollback();
throw;
}


In this scenario if DataComponentA performs rollback transaction everything will be OK, but if BusinessComponent tries to do a Rollback again (in a nested transaction) then we have the problem of my question. How can I know that there is an error in the thread. Is there a way to know this?

You'll have to set at flag or something in your catch block.

PeterRitchie at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 5

So there is no information in the thread, the stack trace, the app domain that states if an untrapped exception has occurred?

JuanRoman at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 6
Juan Roman wrote:
So there is no information in the thread, the stack trace, the app domain that states if an untrapped exception has occurred
If it's un-trapped your application exits. I don't know if the ApplicationExit event is raised; but, it doesn't have any included information about why the application exited.
PeterRitchie at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 7
There are events on the Application and AppDomain for entirely unhandled exceptions.

Won't be triggered if you are mean that the exception isn't handled for a couple of tiers of the stack but is handled lower down.

DuncanWoods at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 8

Duncan Woods wrote:
There are events on the Application and AppDomain for entirely unhandled exceptions.

Won't be triggered if you are mean that the exception isn't handled for a couple of tiers of the stack but is handled lower down.
Yes, there is the UnhandledException event; but, the application is still terminating.

The UnhandledException event is on the AppDomain object; there can be more than one AppDomain per process, this event is only raised for the AppDomain created by the CLR upon invocation of the application. Your application will be in an unknown state when this event is raised, there's not much you can reliably do when handling this event.

There's also the ThreadException event on the Application object that will get raised if an untrapped exception is raised on a thread. As the documentation describes; your thread will be in an unknown state--there's not much you can reliably do in this event handler. It signals that your thread is about to terminate.

[Edit: To be clear; in these event handlers you wouldn't have access to an object to call RollbackTransaction, and quite possibly the object may have already been disposed/finalized.]

PeterRitchie at 2007-8-30 > top of Msdn Tech,.NET Development,Common Language Runtime...

.NET Development

Site Classified