How to catch this exception

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;
}
}
}
}
 
M

Marina

There is no error thrown until the method runs. So at the time of
InitializeComponent, all that happens is that event handlers are hooked up.
This is not the same as running them. So there is no error here.

Now, the method does get run in dg1_ItemDataBound. You are catching the
exception.

However, the catch block is rethrowing the same exception. So now there it
is again.

And after that, there is no code that can handle it that you have written.

So what needs to happen is that your catch block needs to deal with the
exception, and NOT throw it again. And if you do throw it again, then
expect to get an error on your page.

You may also add a Page_Error handler which can handle any exception on your
page (no matter what threw it). Also, you can handle Application_Error in
global.asax, to handle an unhandled exception anywhere in the application.

will said:
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;
}
}
}
}
 
W

will

Thanks, Marina, for responding.
That all makes sense, but I'm a tad confused.
In my app, I have a simple error handling method that checks for an SqlException InnerException (and acts on the error if it is severe). The method then "rethrows" the exception so the exception can be bubble up and be handled at the "top" level of my form's class. This way, I can catch exceptions throughtout the class and know that they will bubble up where I can choose how to deal with them in one place.

If I shouldn't rethrow the exception, how can the exception bubble it's way up?

Thanks for the help.
There is no error thrown until the method runs. So at the time of
InitializeComponent, all that happens is that event handlers are hooked up.
This is not the same as running them. So there is no error here.

Now, the method does get run in dg1_ItemDataBound. You are catching the
exception.

However, the catch block is rethrowing the same exception. So now there it
is again.

And after that, there is no code that can handle it that you have written.

So what needs to happen is that your catch block needs to deal with the
exception, and NOT throw it again. And if you do throw it again, then
expect to get an error on your page.

You may also add a Page_Error handler which can handle any exception on your
page (no matter what threw it). Also, you can handle Application_Error in
global.asax, to handle an unhandled exception anywhere in the application.

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;
}
}
}
}
 
T

Tommy

You can handle the Page object's "Error" event. This event is fired
when an unhandled exception bubbles up.

Sample:

Private Sub Page_Error(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Error
Response.Write(Server.GetLastError.Message())
Server.ClearError()
End Sub

Tommy,

will said:
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;
}
}
}
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top