473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to catch this exception

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
3 1822
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
7
by: Noor | last post by:
please tell the technique of centralize exception handling without try catch blocks in c#.
5
by: Jacek Dziedzic | last post by:
Hi! In my main() function I have a last-resort exception construct that looks like this: int main() { try { // ... program code }
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
2
by: Ralph Krausse | last post by:
I created a try/catch/finally but when an expection is thrown, the catch does not handle it... (I know this code is wrong, I want to force the error for this example) try { DataSet ds = new...
11
by: l.woods | last post by:
I want to set up my CATCH for a specific exception, but I really don't know which one of the multitude that it is. I am getting the exception now with Catch ex as Exception but I want to be...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.