我如何处理这些异常,以便我的应用程序在没有连接时不会崩溃?
How do I handle these exceptions so that my application doesn't crash when there is no connection?
我有一个应用程序在用户尝试登录时在运行时连接到服务器,如果服务器已启动并且(网络应用程序)处于 运行 则没有问题,但是当服务器处于关闭或桌面应用程序连接到的(网络应用程序)不在应用程序崩溃的服务器上 运行。我正在尝试捕获异常,但应用程序仍然崩溃。
namespace --------
{
public partial class Login : MetroForm
{
public Login()
{
InitializeComponent();
}
public static string token = null;
public static long off_id = -1;
public async void getOfficerToken(dynamic client)
{
var result = await client.officers().token.Get();
if (result.HttpResponseMessage.StatusCode != HttpStatusCode.OK)
{
MessageBox.Show(result.message.ToString());
}
else
{
token = result.token;
off_id = result.officer_id;
this.Hide();
Dashboard obj = new Dashboard();
obj.Show();
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(txtUser.Text + ":" + txtPassword.Text));
var client = new RestClient("http://127.0.0.1:5000/api/v1/", new Dictionary<string, string> { { "Authorization", "Basic " + encoded } });
getOfficerToken(client);
}
catch (Exception ex)
{
connectionlbl.Text = "There is no connection to the server.";
}
}
private void Login_Load(object sender, EventArgs e)
{
this.ActiveControl = btnLogin;
}
}
}
我收到以下错误:
- System.Net.Http.HttpRequestException: 'An error occurred while sending the request.'
- 内部异常:
WebException: 无法连接到远程服务器
SocketException: 由于目标机器主动拒绝,无法建立连接
您没有在正确的地方捕获异常。由于 getOfficerToken
是 async
,这是正在发生的事情:
btnLogin_Click
被执行
getOfficerToken
是 async
,所以它在自己的线程 中关闭 运行
btnLogin_Click
完成执行,因为它没有等待 getOfficerToken
完成,因为它没有 await
ed(好吧,不是真的在等待,阅读异步编程以了解什么是真的在继续)
- 异常在
getOfficerToken
中被抛出,但是 btnLogin_Click
不再执行所以错误不是 "inside" try-catch 块(过度 simplistic/technically 不完全是正确,但你明白了)
简答还包括 getOfficerToken
中的 try-catch 或 await
调用。
public async Task getOfficerToken(dynamic client)
{
var result = await client.officers().token.Get();
if (result.HttpResponseMessage.StatusCode != HttpStatusCode.OK)
{
MessageBox.Show(result.message.ToString());
}
else
{
token = result.token;
off_id = result.officer_id;
this.Hide();
Dashboard obj = new Dashboard();
obj.Show();
}
}
private async void btnLogin_Click(object sender, EventArgs e)
{
try
{
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(txtUser.Text + ":" + txtPassword.Text));
var client = new RestClient("http://127.0.0.1:5000/api/v1/", new Dictionary<string, string> { { "Authorization", "Basic " + encoded } });
await getOfficerToken(client);
}
catch (Exception ex)
{
connectionlbl.Text = "There is no connection to the server.";
}
}
我有一个应用程序在用户尝试登录时在运行时连接到服务器,如果服务器已启动并且(网络应用程序)处于 运行 则没有问题,但是当服务器处于关闭或桌面应用程序连接到的(网络应用程序)不在应用程序崩溃的服务器上 运行。我正在尝试捕获异常,但应用程序仍然崩溃。
namespace --------
{
public partial class Login : MetroForm
{
public Login()
{
InitializeComponent();
}
public static string token = null;
public static long off_id = -1;
public async void getOfficerToken(dynamic client)
{
var result = await client.officers().token.Get();
if (result.HttpResponseMessage.StatusCode != HttpStatusCode.OK)
{
MessageBox.Show(result.message.ToString());
}
else
{
token = result.token;
off_id = result.officer_id;
this.Hide();
Dashboard obj = new Dashboard();
obj.Show();
}
}
private void btnLogin_Click(object sender, EventArgs e)
{
try
{
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(txtUser.Text + ":" + txtPassword.Text));
var client = new RestClient("http://127.0.0.1:5000/api/v1/", new Dictionary<string, string> { { "Authorization", "Basic " + encoded } });
getOfficerToken(client);
}
catch (Exception ex)
{
connectionlbl.Text = "There is no connection to the server.";
}
}
private void Login_Load(object sender, EventArgs e)
{
this.ActiveControl = btnLogin;
}
}
}
我收到以下错误:
- System.Net.Http.HttpRequestException: 'An error occurred while sending the request.'
- 内部异常: WebException: 无法连接到远程服务器 SocketException: 由于目标机器主动拒绝,无法建立连接
您没有在正确的地方捕获异常。由于 getOfficerToken
是 async
,这是正在发生的事情:
btnLogin_Click
被执行getOfficerToken
是async
,所以它在自己的线程 中关闭 运行
btnLogin_Click
完成执行,因为它没有等待getOfficerToken
完成,因为它没有await
ed(好吧,不是真的在等待,阅读异步编程以了解什么是真的在继续)- 异常在
getOfficerToken
中被抛出,但是btnLogin_Click
不再执行所以错误不是 "inside" try-catch 块(过度 simplistic/technically 不完全是正确,但你明白了)
简答还包括 getOfficerToken
中的 try-catch 或 await
调用。
public async Task getOfficerToken(dynamic client)
{
var result = await client.officers().token.Get();
if (result.HttpResponseMessage.StatusCode != HttpStatusCode.OK)
{
MessageBox.Show(result.message.ToString());
}
else
{
token = result.token;
off_id = result.officer_id;
this.Hide();
Dashboard obj = new Dashboard();
obj.Show();
}
}
private async void btnLogin_Click(object sender, EventArgs e)
{
try
{
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(txtUser.Text + ":" + txtPassword.Text));
var client = new RestClient("http://127.0.0.1:5000/api/v1/", new Dictionary<string, string> { { "Authorization", "Basic " + encoded } });
await getOfficerToken(client);
}
catch (Exception ex)
{
connectionlbl.Text = "There is no connection to the server.";
}
}