W
will
Hi all. I've got an question about how to catch an exception.
In Page_Load, I place a DataGrid, dg1, into edit mode. This will
call the method called GenericGridEvent. GenericGridEvent will
call a mehtod called ExceptionGenerator that will generate a run-time exception.
The exception is caught in GenericGridEvent, but it isn't caught in
InitializeComponenet (which doens't suprise me). I can't figure
out where to catch an error that may occur in GenericGridEvent.
As you can see from the code below, I try to catch the exception thrown
by ExceptionGenerator everywhere, but I can't catch it.
Any ideas about how to catch this error? Thanks.
Here is the page's code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace HealthWeb
{
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg1;
private void Page_Load(object sender, System.EventArgs e)
{
try
{
LoadData();
}
catch(Exception ex)
{
throw ex;
}
}
private void LoadData()
{
try
{
DataTable dt = new DataTable("DataTable1");
DataColumn dc;
dc = dt.Columns.Add("Column1");
DataRow newRow;
newRow = dt.NewRow();
newRow["Column1"] = "First Column";
dt.Rows.Add(newRow);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
dg1.DataSource = ds.Tables[0];
dg1.DataBind();
}
catch(Exception ex)
{
throw ex;
}
}
override protected void OnInit(EventArgs e)
{
try
{
InitializeComponent();
base.OnInit(e);
dg1.EditItemIndex = 0;
LoadData();
}
catch(Exception ex)
{
Trace.Warn(ex.Message);
}
}
private void InitializeComponent()
{
try
{
this.dg1.ItemDataBound +=new DataGridItemEventHandler(dg1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
catch(Exception ex)
{
Trace.Warn(ex.Message);
}
}
private void dg1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
try
{
ExceptionGenerator();
}
catch(Exception ex)
{
throw ex;
}
}
private void ExceptionGenerator()
{
try
{
int a = 0;
int b = 10;
int c;
c = b / a;
}
catch(Exception ex)
{
throw ex;
}
}
}
}
In Page_Load, I place a DataGrid, dg1, into edit mode. This will
call the method called GenericGridEvent. GenericGridEvent will
call a mehtod called ExceptionGenerator that will generate a run-time exception.
The exception is caught in GenericGridEvent, but it isn't caught in
InitializeComponenet (which doens't suprise me). I can't figure
out where to catch an error that may occur in GenericGridEvent.
As you can see from the code below, I try to catch the exception thrown
by ExceptionGenerator everywhere, but I can't catch it.
Any ideas about how to catch this error? Thanks.
Here is the page's code
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace HealthWeb
{
public class Test : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg1;
private void Page_Load(object sender, System.EventArgs e)
{
try
{
LoadData();
}
catch(Exception ex)
{
throw ex;
}
}
private void LoadData()
{
try
{
DataTable dt = new DataTable("DataTable1");
DataColumn dc;
dc = dt.Columns.Add("Column1");
DataRow newRow;
newRow = dt.NewRow();
newRow["Column1"] = "First Column";
dt.Rows.Add(newRow);
DataSet ds = new DataSet();
ds.Tables.Add(dt);
dg1.DataSource = ds.Tables[0];
dg1.DataBind();
}
catch(Exception ex)
{
throw ex;
}
}
override protected void OnInit(EventArgs e)
{
try
{
InitializeComponent();
base.OnInit(e);
dg1.EditItemIndex = 0;
LoadData();
}
catch(Exception ex)
{
Trace.Warn(ex.Message);
}
}
private void InitializeComponent()
{
try
{
this.dg1.ItemDataBound +=new DataGridItemEventHandler(dg1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
catch(Exception ex)
{
Trace.Warn(ex.Message);
}
}
private void dg1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
try
{
ExceptionGenerator();
}
catch(Exception ex)
{
throw ex;
}
}
private void ExceptionGenerator()
{
try
{
int a = 0;
int b = 10;
int c;
c = b / a;
}
catch(Exception ex)
{
throw ex;
}
}
}
}