Where is my fault?
I have a class type Exception
public class ApiException : Exception {
public ApiException(string message) : base(message) {
}
}
In some situations I call throw new ApiException("Message");
For example here:
public static async Task<string> ValidateToken(string token) {
Dictionary<string, string> values = new Dictionary<string, string> {
{ "token", token}
};
FormUrlEncodedContent content = new FormUrlEncodedContent(values);
HttpResponseMessage response = await client.PostAsync(Globals.sso, content);
string responseString = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode) {
TokenResp result = JsonConvert.DeserializeObject<TokenResp>(responseString);
if (result.Token != token)
throw new ApiException("Token is invalid");
} else {
NonFieldResponse resp = JsonConvert.DeserializeObject<NonFieldResponse>(responseString);
string msg = null;
foreach (string message in resp.non_field_errors) {
if (msg != null) msg += ", ";
msg += message;
}
throw new ApiException(msg);
}
In somewhere I need to catch
exceptions like here:
try {
Type = ValidateToken(token).Result;
} catch (ApiException ae) {
Console.WriteLine(ae.Message);
} catch (Exception e) {
Console.WriteLine(e.Message);
}
But catch (ApiException ae)
doesn't happens, always catched simple Exception
(where e.GetType()
is AggregateException
and e.InnerException.GetType()
is ApiException
).
How to catch my exception?