我正在使用一個在 ApiKey 不起作用時拋出例外的庫。假設該庫有一個名為 GetAppels() 的方法。如果 ApiKey 無效,則回傳訊息例外,在本例中為“禁止”。
請注意 GetAppels() 正在拋出例外。
現在我想知道 ApiKey 何時無效,以便我可以告訴用戶使用了錯誤的密鑰。
所以我做了這個。
try{
apiTest api = new apiTest(Key.Text);
api.GetAppels();
} catch (Exception) { MessageBox.Show("Wrong key!"); }
但這不起作用,由于某種原因,它繼續在此例外之前拋出另一個例外。這是有道理的,因為 GetAppels 中的 try 比我自己的 try/catch 先進行。我怎么能解決這個問題?當 GetAppels 方法(不是方法本身,而是其中的另一個方法)拋出例外時,我如何才能捕獲?
編輯:
GetAppels() 方法只是一個例子,這就是該方法實際拋出例外的方式。
protected void HandleRequestFailure(HttpResponseMessage response)
{
try
{
if (response.StatusCode == (HttpStatusCode)429)
{
var retryAfter = TimeSpan.Zero;
if (response.Headers.TryGetValues("Retry-After", out var retryAfterHeaderValues))
{
if (int.TryParse(retryAfterHeaderValues.FirstOrDefault(), out var seconds))
{
retryAfter = TimeSpan.FromSeconds(seconds);
}
}
string rateLimitType = null;
if (response.Headers.TryGetValues("X-Rate-Limit-Type", out var rateLimitTypeHeaderValues))
{
rateLimitType = rateLimitTypeHeaderValues.FirstOrDefault();
}
throw new RiotSharpRateLimitException("429, Rate Limit Exceeded", response.StatusCode, retryAfter, rateLimitType);
}
else if (RiotHttpStatusCodeResponse.Contains(response.StatusCode))
{
string message;
try // try get error message from response
{
var json = response.Content.ReadAsStringAsync().Result;
var obj = JObject.Parse(json);
message = obj["status"]["message"].ToObject<string>();
}
catch {
message = response.StatusCode.ToString();
}
throw new RiotSharpException(message, response.StatusCode);
}
else
throw new RiotSharpException("Unexpeced failure", response.StatusCode);
}
finally
{
response.Dispose(); //Dispose Response On Error
}
}
這就是我捕捉例外的方式,也許我做錯了什么。
private bool CheckAPIKEY(string key)
{
try
{
riotApi = RiotApi.GetDevelopmentInstance(key);
riotApi.Summoner.GetSummonerByNameAsync(
region: RiotSharp.Misc.Region.Euw,
summonerName: summonerName
);
return true;
}
catch (RiotSharpException) { return false; }
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (gameIsRunning)
MessageBox.Show("Program is already running!");
else if (summonerName != null)
{
KEY = Key.Text;
if(!CheckAPIKEY(KEY))
MessageBox.Show("Your Riot API Key isn't valid.");
}
else
MessageBox.Show("Couldn't recive information about your SummonerName, is your League of legends game opened?");
}
uj5u.com熱心網友回復:
您正在呼叫異步方法并且不以任何方式等待結果。您可以執行以下任一操作,但我認識的大多數 .NET 開發人員都首選第一個。
private async Task<bool> CheckAPIKEY(string key)
{
try
{
riotApi = RiotApi.GetDevelopmentInstance(key);
await riotApi.Summoner.GetSummonerByNameAsync(
region: RiotSharp.Misc.Region.Euw,
summonerName: summonerName
);
return true;
}
catch (RiotSharpException) { return false; }
}
或這個
private bool CheckAPIKEY(string key)
{
try
{
riotApi = RiotApi.GetDevelopmentInstance(key);
riotApi.Summoner.GetSummonerByNameAsync(
region: RiotSharp.Misc.Region.Euw,
summonerName: summonerName
)
.GetAwaiter()
.GetResult();
return true;
}
catch (RiotSharpException) { return false; }
}
uj5u.com熱心網友回復:
像這樣嘗試
private bool API(string a)
{
try
{
Api = WebApi.GetDevelopmentInstance(a);
Api.Summoner.GetSummonerByNameAsync(
region: WebApi.Misc.Region.Euw,
summonerName: summonerName
)
.GetAwaiter()
.GetResult();
return true;
}
catch (WebApiException) { return false; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/362412.html
上一篇:拋出例外而沒有任何真正的錯誤?
下一篇:編輯System.NullReferenceException時使用DataTable的Datagrid系結:“未將物件參考設定為物件的實體。”
