By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,843 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,843 IT Pros & Developers. It's quick & easy.

How to catch this exception

P: n/a
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;
}
}
}
}
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <gr****@willwyatt.com> wrote in message
news:nURTb.13472$EW.1572@okepread02...
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;
}
}
}
}

Nov 18 '05 #2

P: n/a
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.

Marina wrote:
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" <gr****@willwyatt.com> wrote in message
news:nURTb.13472$EW.1572@okepread02...
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;
}
}
}
}


Nov 18 '05 #3

P: n/a
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 <gr****@willwyatt.com> wrote in message news:<nURTb.13472$EW.1572@okepread02>...
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;
}
}
}
}

Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.