I am facing little problem. When I got an error then I want to skip some steps so I've used goto.
At label I have another try-catch and if I pass through first try catch then it continues catch in second try-catch..!!!!
So, how to end first catch?
Here is my code for reference:
try{odc.Open();}catch{body+="<br /><b>Can not open MySQL connection!</b>";goto ExitToEmail;}
Some steps(
Step1,
Step2,
.
.
.)
ExitToEmail:
Mail.SendMail("abc@xyz.com", "Support Team", "notf@xyz.com", body, "Report","","","", null);
Above mentioned method from another class file, here is the code:
public static bool SendMail(string from, string fromname, string to, string body, string subject, string mailcc, string mailbcc, string attachment, Attachment attch)
{
try{Steps...}
catch (Exception ex){"If Connection can't be open here I also get exception..."}
}
goto belongs in the old age, you might want to avoid it and code your way around it with if/else and function call.
- change the code portion of ExitTomail into a function; e.g. SendMail
- in the catch, call SendMail and call return; to exit your function. If you don't want to call the SendMail function within the catch, use a flag (bool error).
Suraj, code design requires reasonable separation from one component to others.
For example:
(a) keep your e-mail method 100% isolated ... you can do this by having it return a Boolean plus one or more out parameters, sample:
public class SurajEmailMethods
{
public static Boolean surajEmailer(
String from, String fromname,
String to, String body,
String subject,
String mailcc, String mailbcc,
String attachment, System.Net.Mail.Attachment attch,
out List<String> messageList)
{
Boolean success = true; // set to false if any errors
List<String> emailMessageList = new List<String>();
try
{
// ...
}
catch (Exception ex)
{
// ...
emailMessageList.Add(ex.Message);
emailMessageList.Add(ex.StackTrace);
success = false;
}
messageList = emailMessageList; // The out parameter 'messageList' must be assigned to before control leaves the current method
return success;
}
}
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Marked as answer by Mark - MSFT on Mar 05, 2013 02:27 AM
surajjoshi13
Member
11 Points
26 Posts
goto from catch continues catch
Feb 25, 2013 12:32 PM|LINK
Hi,
I am facing little problem. When I got an error then I want to skip some steps so I've used goto.
At label I have another try-catch and if I pass through first try catch then it continues catch in second try-catch..!!!!
So, how to end first catch?
Here is my code for reference:
try{odc.Open();}catch{body+="<br /><b>Can not open MySQL connection!</b>";goto ExitToEmail;} Some steps( Step1, Step2, . . .) ExitToEmail: Mail.SendMail("abc@xyz.com", "Support Team", "notf@xyz.com", body, "Report","","","", null);Above mentioned method from another class file, here is the code:
public static bool SendMail(string from, string fromname, string to, string body, string subject, string mailcc, string mailbcc, string attachment, Attachment attch) { try{Steps...} catch (Exception ex){"If Connection can't be open here I also get exception..."} }sharp11
Member
18 Points
3 Posts
Re: goto from catch continues catch
Feb 25, 2013 01:29 PM|LINK
It is really bad practice to use the 'goto' statement, try to replace with if-else or while loops with 'break's.
Try something like this:
void somefunction() { bool success = true; try { } catch (Exception ex) { success = false; } if (success) { //do something } else { SendMail(1,2,3,..); } }Paul Linton
Star
13581 Points
2571 Posts
Re: goto from catch continues catch
Feb 25, 2013 08:41 PM|LINK
I don't understand totally but have you tried single stepping in the debugger?
Are you sure that you don't just get another execption in the SendMail code which triggers the catch in that method?
But, really, why not just send the email from the first catch handler and then exit the method? What is the point of of the goto?
WishStar99
Contributor
2857 Points
879 Posts
Re: goto from catch continues catch
Feb 26, 2013 04:21 PM|LINK
goto belongs in the old age, you might want to avoid it and code your way around it with if/else and function call.
- change the code portion of ExitTomail into a function; e.g. SendMail
- in the catch, call SendMail and call return; to exit your function. If you don't want to call the SendMail function within the catch, use a flag (bool error).
if(error == false)
{
some steps( ... step 1, step 2 ...
}
// then send mail at the end of the function
SendMail
gerrylowry
All-Star
20577 Points
5721 Posts
Re: goto from catch continues catch
Feb 28, 2013 05:57 AM|LINK
@ surajjoshi13 welcome to forums.asp.net
TIMTOWTDI =. there is more than one way to do it
Suraj, code design requires reasonable separation from one component to others.
For example:
(a) keep your e-mail method 100% isolated ... you can do this by having it return a Boolean plus one or more out parameters, sample:
public class SurajEmailMethods { public static Boolean surajEmailer( String from, String fromname, String to, String body, String subject, String mailcc, String mailbcc, String attachment, System.Net.Mail.Attachment attch, out List<String> messageList) { Boolean success = true; // set to false if any errors List<String> emailMessageList = new List<String>(); try { // ... } catch (Exception ex) { // ... emailMessageList.Add(ex.Message); emailMessageList.Add(ex.StackTrace); success = false; } messageList = emailMessageList; // The out parameter 'messageList' must be assigned to before control leaves the current method return success; } }you would call it with code like this ...
try { odc.Open(); } catch (Exception ex) { body += ... if(!SurajEmailMethods.surajEmailer("", ... ) { // ... handle error appropriately } }g.