Weekly C# & ASP.NET Question : 24th Nov 2011--- Nested Exceptions

Right, while Mr @Rad is AWOL and he probably cracking some code for a mailing list , I'll keep this thread going until day of changeover. In the meantime, skunks please bear with this thread. Since am already aware of how and where exceptions work, what is to be used and what exceptions that am testing for. Good coders will ( can't believe am saying this..) create a general rule of the thumb try/catch blocks for most tasks in the code. So here is my question : What would be the correct format for nested TRY-CATCH blocks in the same method? Would it be : a) TRY/CATCH/CATCH/CATCH etc or would it be : b) TRY/CATCH, TRY/CATCH etc Rgds. :-) -- **--If I ever wrote code on a system for the better of consumer freedoms or choice, I'd rather create the complete system with people who share the same views than give out a single line of code to code thieves who will use it to defeat the principles of freedom and choices---2011--Me--**

How about something like this try { //main code try { //child code } catch(Exception ex) { //display error message from child code } } catch(Exception ex) { //display error message from main code }

Brilliant and makes a lot of sense. A question. Suppose the code breaks in the main Try, how will it exit to the main catch and not go to the next line of the child try/catch block because that is the next code block? Rgds. On Thu, Nov 24, 2011 at 10:51 PM, Daniel Ndeti <dantoz@gmail.com> wrote:
How about something like this try { //main code try { //child code } catch(Exception ex) { //display error message from child code } } catch(Exception ex) { //display error message from main code }

Add a finally clause after the catch. that should do the trick try { //main code try { //child code } catch(Exception ex) { //display error message from child code } } catch(Exception ex) { //display error message from main code } finally { //exit the code block }

Great input Daniel. Your implementation structure of the Nested Try/Catch blocks is another option that is useful and would work side by side with both Options A & B in the original thread. Rgds. :-) On Fri, Nov 25, 2011 at 12:11 AM, Daniel Ndeti <dantoz@gmail.com> wrote:
Add a finally clause after the catch. that should do the trick

@Aki, @Daniel First, it is not good practice to only catch the generic "Exception" - you should catch a specific Exception THEN catch Exception at the end And the kind of nesting you use depends on whether you can handle the exception and allow execution to proceed or not e.g. try { //a. simple exception is thrown but i can handle and continue execution try { } catch (SimpleException ex) { //handle and let execution continue } //b. some weird exception thrown here //dont want anything beyond this point to execute if any WeirdException was thrown } catch(SomeWeirdException swe) { } catch(AnotherWeirdException awe) { } catch(Exception ex) { } finally { //clean up before exit } -Billy

In an real environment situation your example should be the best way forward. Take note AKI On Fri, Nov 25, 2011 at 11:20, Billy <billyx5@gmail.com> wrote:
@Aki, @Daniel First, it is not good practice to only catch the generic "Exception" - you should catch a specific Exception THEN catch Exception at the end And the kind of nesting you use depends on whether you can handle the exception and allow execution to proceed or not e.g.
try {
//a. simple exception is thrown but i can handle and continue execution try {
} catch (SimpleException ex) { //handle and let execution continue
}
//b. some weird exception thrown here
//dont want anything beyond this point to execute if any WeirdException was thrown
} catch(SomeWeirdException swe) {
} catch(AnotherWeirdException awe) {
} catch(Exception ex) {
} finally { //clean up before exit
}
-Billy
_______________________________________________ Skunkworks mailing list Skunkworks@lists.my.co.ke ------------ List info, subscribe/unsubscribe http://lists.my.co.ke/cgi-bin/mailman/listinfo/skunkworks ------------
Skunkworks Rules http://my.co.ke/phpbb/viewtopic.php?f=24&t=94 ------------ Other services @ http://my.co.ke

@Billy, thnks for adding your implementation and you are absolutely correct. That makes it 4 different ways to use the try/catch blocks. Nice, we should pin this up somewhere for a quick reference in the future for the community use. Note that I did not even mention about Googling for inputs to this thread because if we understand the concepts, we can create more powerful features that are user friendly. For example, using other exception handlers, you can catch DNS and TCP errors to a certain level. If implemented correctly, assume that the end user launched the program and forgot to connect their internet connection, the result would be more user friendly than just having a blank page which stares at the user. Great stuff. :-) On Fri, Nov 25, 2011 at 11:20 AM, Billy <billyx5@gmail.com> wrote:
@Aki, @Daniel First, it is not good practice to only catch the generic "Exception" - you should catch a specific Exception THEN catch Exception at the end And the kind of nesting you use depends on whether you can handle the exception and allow execution to proceed or not

I always preffer building my own error handling class for common user errors then give the user meaningful messages that they can be able to understand coz at the end of the day,the user wont understand what "index of user_emailaddress is not unique" means but with something like "The email address exists in the system" will sure save you alot of calls. Your thots?

Nice, the more power to a friendly user interface, the better. Infact, I hope someday we make the time, it would be nice to build a fully scaled inter-active feedback system for users. Imagine someone went to an ATM machine and was greated with a nice big smile, welcome message."Please take your money from me..., with a reminder for a bithday greeting or anniversary" The machine in the metal case concept needs to go much higher. Our programming platform makes it even easier to do this on Windows Embedded or even the web. Which takes me a bit further and maybe out of thread, anyone of you tried C# on windows embedded and care to share expectations versus limitations experiences? This embedded platform is still waiting to be utilized in many areas such as security systems and display facilities. Maybe we can use this thread as a next week topic question.. On Fri, Nov 25, 2011 at 12:43 PM, Daniel Ndeti <dantoz@gmail.com> wrote:
I always preffer building my own error handling class for common user errors then give the user meaningful messages that they can be able to understand coz at the end of the day,the user wont understand what "index of user_emailaddress is not unique" means but with something like "The email address exists in the system" will sure save you alot of calls.
Your thots?
participants (3)
-
aki
-
Billy
-
Daniel Ndeti