how to throw (raise) exception from a custom control

Discussion in 'ASP .Net Building Controls' started by Gopal Krish, Oct 18, 2004.

  1. Gopal Krish

    Gopal Krish Guest

    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
     
    Gopal Krish, Oct 18, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
     
    Victor Garcia Aprea [MVP], Oct 23, 2004
    #2
    1. Advertising

  3. Gopal Krish

    Gopal Krish Guest

    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 [MVP]" <> wrote in message news:<#>...
    > 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" <> wrote in message
    > news:...
    > > 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
     
    Gopal Krish, Oct 24, 2004
    #3
  4. 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" <> wrote in message
    news:...
    > 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 [MVP]" <> wrote in message
    > news:<#>...
    >> 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" <> wrote in message
    >> news:...
    >> > 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
     
    Victor Garcia Aprea [MVP], Oct 24, 2004
    #4
  5. Gopal Krish

    Gopal Krish Guest

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

    Thanks
    Paul

    "Victor Garcia Aprea [MVP]" <> wrote in message news:<u8#>...
    > 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" <> wrote in message
    > news:...
    > > 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 [MVP]" <> wrote in message
    > > news:<#>...
    > >> 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" <> wrote in message
    > >> news:...
    > >> > 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
     
    Gopal Krish, Oct 25, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    13,116
    Kevin Spencer
    Oct 27, 2003
  2. Replies:
    15
    Views:
    7,760
    Roedy Green
    Sep 8, 2005
  3. Jacol

    raise or not to raise [Newbie]

    Jacol, Feb 3, 2007, in forum: Python
    Replies:
    5
    Views:
    433
    Gabriel Genellina
    Feb 5, 2007
  4. ernest
    Replies:
    2
    Views:
    308
    Roy Smith
    Nov 14, 2010
  5. Jack Bates
    Replies:
    0
    Views:
    295
    Jack Bates
    May 2, 2011
Loading...

Share This Page