DataTable RowChanged event swallowing exceptions?

G

Guest

Is it a bug or by design that the RowChanged event swallows all exceptions
which are thrown on event handlers? I would have expected an Unhandled
Exception. The following code reproduces this behaviour.

class DataTableException
{
[STAThread]
static void Main(string[] args)
{
// Create table
DataTable table = new DataTable();

// Create and add column
DataColumn column = table.Columns.Add();
column.DataType = typeof(string);

// Add a row
table.Rows.Add(new object[]{"Test"});

// Adding Event Handler
table.RowChanged += new
DataRowChangeEventHandler(Table_DataRowChanged);

// Changing Row
table.Rows[0][0] = "Changing";

Console.WriteLine("Finished");
}

private static void Table_DataRowChanged(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine("Row Changed : Action " + e.Action);
Console.WriteLine("Throwing Exception");

// Throwing exception
throw new Exception("This is an exception.");
}


I have used .NET Reflector to look at the RaiseRowChanged method on the
DataTable and found the following code for rows being updated. (V1.1)

try
{
this.OnRowChanged(e);
}
catch (Exception exception1)
{
ExceptionBuilder.Trace(exception1);
}
 
K

Kawarjit Bedi [MSFT]

DataTable's RowChangedEvent catches all exceptions that may get raised in
the associated EventHandler. All User exceptions are suppressed but System
exceptions like StackOverflow, OutOfMemory, ThreadAbort, NullReference,
AcessViolation are re-thrown.

This behavior is by design.

We recommend that data validation and other business logic be executed in
RowChangingEvent and if any user exceptions needs to be raised in the
EventHandler, it will ripple up.


Thanks,
Kawarjit Bedi [MSFT]

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top