Reputation: 4607
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?
Upvotes: 2
Views: 28971
Reputation: 414
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)
Upvotes: 2
Reputation: 8451
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.
Upvotes: 7
Reputation: 6105
try
{
net.doRequest();
}
catch(Exception e)
{
if(e is WebException)
{
//handle appropriately
return; //will exit the method
}
else
throw; //good idea to throw unexpected exceptions anyway
}
Upvotes: 1
Reputation: 12654
You can catch exception, do something with it, and then rethrow it, so that no further code from this method will get executed.
try{
...
}catch(Exception ex){
// do something with ex
throw;
}
Upvotes: 1