Let us say that I have two classes. Here is the relevant code for the first class called Internet:
public void doRequest()
{
string URL = "http://localhost:4000/HomePage.aspx";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
public void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
byte[] byteArray = Encoding.UTF8.GetBytes("Test Message");
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
}
public void GetResponsetStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.EndGetResponse(callbackResult);
StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream());
string result = httpWebStreamReader.ReadToEnd();
}
catch (WebException)
{
Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show("Error: Could not reach the web service"));
}
}
This code is being called by a class called MainPage.xaml.cs:
private void Button_LogIn_Click(object sender, RoutedEventArgs e)
{
Internet net = new Internet();
bool check = net.checkInternet();
if (check.Equals(false))
{
}
else
{
net.doRequest();
ClientServiceSoapClient web_service = new ClientServiceSoapClient();
web_service.LogInCompleted += new System.EventHandler<LogInCompletedEventArgs>(LogInComplete);
web_service.LogInAsync(TextBox_Username.Text, TextBox_Password.Password);
}
}
What I want is that if an exception occurs in the doRequest method call, the exception is handled (WebException at the end of the Internet class) and then STOP executing the code after it (ClientServiceSoapClient web_service....).
How can I do this?
You can use the keyword return here (which will return void). I've actually used it twice, you have an empty if statement so I've rejigged it a little.
private void Button_LogIn_Click(object sender, RoutedEventArgs e)
{
Internet net = new Internet();
bool check = net.checkInternet();
if (check.Equals(false))
return;
try
{
await net.doRequest(); //UPDATED THIS LINE
ClientServiceSoapClient web_service = new ClientServiceSoapClient();
web_service.LogInCompleted += new System.EventHandler<LogInCompletedEventArgs>(LogInComplete);
web_service.LogInAsync(TextBox_Username.Text, TextBox_Password.Password);
}
catch
{
//do not throw an error, instead just use the return keyword!
return;
}
}
However, you are using an async method which doesn't make sense to me! It should not be called Asynchronously if you need to validate the result before continuing, it should be done in serial. Or, review the await keyword.
The title is asking to stop execution and not returning from a function. So if you are not in the main function, and you don't want to throw an exception and then catch it again in main, you can use Environment.Exit() with an Exit code.. https://msdn.microsoft.com/en-us/library/ms681382(v=vs.85)
Environment.Exit(ERROR_INVALID_COMMAND_LINE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With