473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception handling in web user control

Hi,
I created a user control, but instead of handling exception in the
user control level I prefer to handle them in the containing form.

In the load event of the containing form I'm using:
entryWindow.Err or += new EventHandler(Pa ge_Error);

when entryWindow is my user control.

Two questions:
1. Is this the correct way of doing that ?
2. When exception occurs I'm setting some of my controls' visibility
property to false, however it seems that asp.net ignores it and still
displays them, why ?

Thank you,
Efi

May 6 '07 #1
1 2909
"Efi Merdler" <fo****@gmail.c omwrote in message
news:11******** *************@e 65g2000hsc.goog legroups.com...
Hi,
I created a user control, but instead of handling exception in the
user control level I prefer to handle them in the containing form.

In the load event of the containing form I'm using:
entryWindow.Err or += new EventHandler(Pa ge_Error);

when entryWindow is my user control.

Two questions:
1. Is this the correct way of doing that ?
2. When exception occurs I'm setting some of my controls' visibility
property to false, however it seems that asp.net ignores it and still
displays them, why ?
It looks like you are violating encapsulation. You are giving the containing
control too much knowledge of the internals of the user control. You're
asking the container to understand the exceptions thrown by the user
control. You might get away with that right now, when there's only one
possible containing control, but you should ask yourself what would happen
if you needed to use that user control in another container. Wouldn't the
other container also need that same knowledge of the user control?

Instead, why not let the user control handle its own exceptions and do with
them what needs to be done. For instance, if there's a place in the user
control where some controls are databound, and there's an exception thrown
while accessing the database, then the user control itself should perhaps
disable the controls which would have been data bound. That's not something
that a containing page should have to know about.

Personally, I never use the Error event of a page. Instead, I use try/catch
blocks, and only catch exceptions if I'm going to do something intelligent
with them. That could be something as simple as logging the exception and
displaying an error label; to adding information to the exception and
rethrowing. In rare cases, one might catch an exception that means that the
user entered data which has led to an "expected error" result. An example
might be the user entering data which would result in a database constraint
violation. In that case, I would catch the exception and turn it into a
purely UI event, possibly displaying an error message.

If the containing control is calling a method or property of the user
control, and that method or property throws an unhandled exception, _then_
the containing control should handle the exception:

string lookupKey = Request.QuerySt ring["LookupKey"];
try
{
myUserControl.L ookupKey = lookupKey;
myUserControl.D ataBind();
}
catch (DataException dEx) // Note that if it threw NullReferenceEx ception,
for instance, then global error handling would deal with it
{
lblError.Text = "No such key " + lookupKey;
lblError.Visibl e = true;
}

In any case, I suggest you keep exception handling as local as possible, and
limit it to only those exceptions you know what to do about.
--
John Saunders [MVP]
May 6 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1792
by: Dan Bass | last post by:
In a somewhat complex application, I've developed plug-in architecture and am having a problem as to when to catch general exceptions for logging purposes. In each plug-in class library, for example, I'm assuming it's throw on errors, and that the core then catches them. I've got proxy classes handling some fiddly details of the plug-ins. Should I allow the exception to bubble to the top most point it can in a call stack, then catch...
44
4207
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level user tasks should always be included in a try/catch block that actually handles any exceptions that occur (log the exception, display a message box, etc.). 2. Low-level operations that are used to carry out the high level tasks
2
5865
by: Rajeev Soni | last post by:
Hi, Considering the scenario for handling exceptions in Web Application where we have Presentation layer, Business layer and Data Access layer; if there any exception is occurred in DAL, what is the best thing to do: 1. Dont catch the exception in DAL and let it prop up to the Application level and the Global.Application_Error event log it to any source and show let ASP.NET show custom error page provided in Web.Config file. OR 2....
2
1279
by: | last post by:
I am looking for a easy way to trap an Exception that may occurr in any one of 30 child web controls to be trapped at the topmost parent WebUser control or Page Level. I have tried variations of setting the Error event handler for the topmost parent WebUser control that I want to handle the error to no avail. Sample code from the MSDN seems to imply that this can be done but it can only be taken care of at the same user control level as...
4
1617
by: Prince Mathew | last post by:
Hi All, I have a requirement. I am throwing an exception from the Page_Load of my user control I want to catch this in my container page. Is this possible? The Page_Load of user control is executed after the Page_Load of the page. So we cannot catch this in the Page_Load of the page. So my question where in the page i will catch the exception thrown form the user control. I don't want any event to be raised from the user control.
6
2309
by: lgbjr | last post by:
Hi All, I've posted this question on the ComponentOne news group, but I thought it couldn't hurt to post it here as well, since, I’m assuming, some others out there are using the C1 controls for VB.NET. This is what I have: VB.Net form with a c1FlexGrid and some c1Textboxes C1ExpressConnection to an Access DB C1ExpressTable
11
5575
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
1
3381
by: Tom Berger | last post by:
Just a short question concerning exception handling.... All of my applications contain an event handling in the Main() like this one: static void Main() { // declare global exeption handler Application.ThreadException += new
35
3499
by: eliben | last post by:
Python provides a quite good and feature-complete exception handling mechanism for its programmers. This is good. But exceptions, like any complex construct, are difficult to use correctly, especially as programs get large. Most of the issues of exceptions are not specific to Python, but I sometimes feel that Python makes them more acute because of the free-n- easy manner in which it employs exceptions for its own uses and allows users...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8845
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8622
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.