how to throw (raise) exception from a custom control

G

Gopal Krish

Here is an interesting situation I'm facing while creating custom
controls.

Problem Abstract:
Unable to throw a exception from within the custom user control I
developed as a DLL.

Details:
I wrote a simple custom user control which creates a text box
dynamically.

Then I added try catch in the custom user control and deliberately
inserted a run time error to test the exception handling from custom
user control back to the calling web page.

The custom user control code is as follows and is very simple

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
try
{
int[] arr = new int[2]{1,2};
TextBox myTextBox = new TextBox();
myTextBox.Text = arr[4].ToString();
Controls.Add(myTextBox);
}
catch(Exception Ex)
{
throw new Exception(Ex.Message);
}
}

protected override void OnPreRender(EventArgs e)
{

}
}
}

As you can see from the code I deliberately introduced a run time
error (arr[4]) to test the error os propagated to the calling web
page.

Now, when I used this control in my web form and try to run it I
expect that the error will be propagated from the user control to my
web page and display is somewhere.

Here is the complete code for my web form's code behind

using ......
namespace WebApplication1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 mySimpleTextControl;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
}
}
}

When I run this code, the error is not propagated back to this page
because, as you can see, there is no place for me to put try catch
because the custom user control is added to this page in design time.
It still throws an unhandled exception at the line "throw new
Exception(Ex.Message);".

It does not work (even throw an unhandled exception or work correctly)
if I add the custom user control dynamically thru code.

Can someone throw some insights to this behavior?

Thanks
 
G

Gopal Krish

Hi Victor,

I couldn't find a OnError method in the Control or the WebControl
class. I didn't understand what you meant by page onError method. The
error occurs in the aspx page (Page_Load) but the control still goes
to the custom control class.

Any thoughts?


Victor Garcia Aprea said:
Hi Gopal,

You can override the page OnError method and there you will have a chance to
handle the exception thrown by your control.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://clariusconsulting.net/vga
My profile: http://aspnet2.com/mvp.ashx?vga


Gopal Krish said:
Here is an interesting situation I'm facing while creating custom
controls.

Problem Abstract:
Unable to throw a exception from within the custom user control I
developed as a DLL.

Details:
I wrote a simple custom user control which creates a text box
dynamically.

Then I added try catch in the custom user control and deliberately
inserted a run time error to test the exception handling from custom
user control back to the calling web page.

The custom user control code is as follows and is very simple

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
try
{
int[] arr = new int[2]{1,2};
TextBox myTextBox = new TextBox();
myTextBox.Text = arr[4].ToString();
Controls.Add(myTextBox);
}
catch(Exception Ex)
{
throw new Exception(Ex.Message);
}
}

protected override void OnPreRender(EventArgs e)
{

}
}
}

As you can see from the code I deliberately introduced a run time
error (arr[4]) to test the error os propagated to the calling web
page.

Now, when I used this control in my web form and try to run it I
expect that the error will be propagated from the user control to my
web page and display is somewhere.

Here is the complete code for my web form's code behind

using ......
namespace WebApplication1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 mySimpleTextControl;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
}
}
}

When I run this code, the error is not propagated back to this page
because, as you can see, there is no place for me to put try catch
because the custom user control is added to this page in design time.
It still throws an unhandled exception at the line "throw new
Exception(Ex.Message);".

It does not work (even throw an unhandled exception or work correctly)
if I add the custom user control dynamically thru code.

Can someone throw some insights to this behavior?

Thanks
 
V

Victor Garcia Aprea [MVP]

Hi Gopal,

If I'm following you, what you need is a way to catch an exception throw by
one of your custom controls, and catch it at the page level, is that right?

If so, you can attach to the Error event or (what I told you before) just
override the OnError method of Page class.

A year ago I wrote a small post on the Error event and some of its
particularitites, you can find it here[1]

[1] http://weblogs.asp.net/vga/archive/2003/06/16/8748.aspx


--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://clariusconsulting.net/vga
My profile: http://aspnet2.com/mvp.ashx?vga

Gopal Krish said:
Hi Victor,

I couldn't find a OnError method in the Control or the WebControl
class. I didn't understand what you meant by page onError method. The
error occurs in the aspx page (Page_Load) but the control still goes
to the custom control class.

Any thoughts?


Victor Garcia Aprea said:
Hi Gopal,

You can override the page OnError method and there you will have a chance
to
handle the exception thrown by your control.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://clariusconsulting.net/vga
My profile: http://aspnet2.com/mvp.ashx?vga


Gopal Krish said:
Here is an interesting situation I'm facing while creating custom
controls.

Problem Abstract:
Unable to throw a exception from within the custom user control I
developed as a DLL.

Details:
I wrote a simple custom user control which creates a text box
dynamically.

Then I added try catch in the custom user control and deliberately
inserted a run time error to test the exception handling from custom
user control back to the calling web page.

The custom user control code is as follows and is very simple

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
try
{
int[] arr = new int[2]{1,2};
TextBox myTextBox = new TextBox();
myTextBox.Text = arr[4].ToString();
Controls.Add(myTextBox);
}
catch(Exception Ex)
{
throw new Exception(Ex.Message);
}
}

protected override void OnPreRender(EventArgs e)
{

}
}
}

As you can see from the code I deliberately introduced a run time
error (arr[4]) to test the error os propagated to the calling web
page.

Now, when I used this control in my web form and try to run it I
expect that the error will be propagated from the user control to my
web page and display is somewhere.

Here is the complete code for my web form's code behind

using ......
namespace WebApplication1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 mySimpleTextControl;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
}
}
}

When I run this code, the error is not propagated back to this page
because, as you can see, there is no place for me to put try catch
because the custom user control is added to this page in design time.
It still throws an unhandled exception at the line "throw new
Exception(Ex.Message);".

It does not work (even throw an unhandled exception or work correctly)
if I add the custom user control dynamically thru code.

Can someone throw some insights to this behavior?

Thanks
 
G

Gopal Krish

Thanks Victor...I understand it now and its working great.

Thanks
Paul

Victor Garcia Aprea said:
Hi Gopal,

If I'm following you, what you need is a way to catch an exception throw by
one of your custom controls, and catch it at the page level, is that right?

If so, you can attach to the Error event or (what I told you before) just
override the OnError method of Page class.

A year ago I wrote a small post on the Error event and some of its
particularitites, you can find it here[1]

[1] http://weblogs.asp.net/vga/archive/2003/06/16/8748.aspx


--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://clariusconsulting.net/vga
My profile: http://aspnet2.com/mvp.ashx?vga

Gopal Krish said:
Hi Victor,

I couldn't find a OnError method in the Control or the WebControl
class. I didn't understand what you meant by page onError method. The
error occurs in the aspx page (Page_Load) but the control still goes
to the custom control class.

Any thoughts?


Victor Garcia Aprea said:
Hi Gopal,

You can override the page OnError method and there you will have a chance
to
handle the exception thrown by your control.

--
Victor Garcia Aprea
Microsoft MVP | ASP.NET
Looking for insights on ASP.NET? Read my blog:
http://clariusconsulting.net/vga
My profile: http://aspnet2.com/mvp.ashx?vga


Here is an interesting situation I'm facing while creating custom
controls.

Problem Abstract:
Unable to throw a exception from within the custom user control I
developed as a DLL.

Details:
I wrote a simple custom user control which creates a text box
dynamically.

Then I added try catch in the custom user control and deliberately
inserted a run time error to test the exception handling from custom
user control back to the calling web page.

The custom user control code is as follows and is very simple

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace WebControlLibrary1
{
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
protected override void CreateChildControls()
{
try
{
int[] arr = new int[2]{1,2};
TextBox myTextBox = new TextBox();
myTextBox.Text = arr[4].ToString();
Controls.Add(myTextBox);
}
catch(Exception Ex)
{
throw new Exception(Ex.Message);
}
}

protected override void OnPreRender(EventArgs e)
{

}
}
}

As you can see from the code I deliberately introduced a run time
error (arr[4]) to test the error os propagated to the calling web
page.

Now, when I used this control in my web form and try to run it I
expect that the error will be propagated from the user control to my
web page and display is somewhere.

Here is the complete code for my web form's code behind

using ......
namespace WebApplication1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 mySimpleTextControl;
protected System.Web.UI.WebControls.Label Label1;

private void Page_Load(object sender, System.EventArgs e)
{
}
}
}

When I run this code, the error is not propagated back to this page
because, as you can see, there is no place for me to put try catch
because the custom user control is added to this page in design time.
It still throws an unhandled exception at the line "throw new
Exception(Ex.Message);".

It does not work (even throw an unhandled exception or work correctly)
if I add the custom user control dynamically thru code.

Can someone throw some insights to this behavior?

Thanks
 

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,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top