Created your exception class

public class HTML_Manager_Exception : ApplicationException { public HTML_Manager_Exception() { } public HTML_Manager_Exception(string message) : base(message) { } public HTML_Manager_Exception(string message, Exception inner) : base(message, inner) { } protected HTML_Manager_Exception(SerializationInfo info, StreamingContext context) : base(info, context) { } } 

I have two methods:

  public static async void BonusTableRefresh(int u_id) { try { LoginBon(u_id); } catch (HTML_Manager_Exception ex) { throw new HTML_Manager_Exception(ex.Message); } } public static async void LoginBon(int u_id) { string login = CryMotherFucker.DecryptStringAES(du["log_bonus"].ToString(), CryMotherFucker.SharedSecret); if (string.IsNullOrEmpty(login)) throw new HTML_Manager_Exception("Для пользователя не указан логин доступа к сайту"); } 

I need that when throwing an exception in the LoginBon method, it is passed to the BonusTableRefresh method, but for some reason this does not happen, the program simply stops. What's the matter?

  • 2
    You cannot pass an exception to a method that does not wait for a result. - Pavel Mayorov

1 answer 1

If your methods are really asynchronous, then you need to call them as asynchronous, waiting for the execution result. In particular, instead of

 LoginBon(u_id); 

write

 await LoginBon(u_id); 

And replace the type of the return value for it from void to Task. Also, if the CryMotherFucker.DecryptStringAES method is asynchronous, then an await operator is needed before it.