Exception coming from a LIB and closing the system c#

Asked

Viewed 31 times

0

I made a lib to manage the part of a system of mine, until then everything is ok, the problem occurs when this lib launches an Exception:

public async void Update(ObjetoPostalModel objeto)
    {
            await LogarAsync(0);

            if (Validacao.Sucesso == false)
            {
                throw new Exception(Validacao.Mensagem);
            }
        }

At that moment the system closes, as it launched this Exception, how do I return the error message:

I’m using this Lib as follows:

private async Task AtualizaUpdate()
    {
        void run()
        {
            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
                {
                    lib.AguardandoUpdate++;
                    lib.Update();

                }));
            }
        }
        Task task = Task.Run(run);
        await Task.WhenAll(task);

    }

To call this method within my system I use:

 try
        {
            await AtualizaUpdate();
            lib.AguardandoUpdate--;
        }
        catch (Exception ex)
        {
           lib.AguardandoUpdate--;
            NotificacaoUtil.Error(ex.Message);

        }

1 answer

-1

Follow an example that solves your problem:

{
    Task<string> theTask = DelayAsync();

    try
    {
        string result = await theTask;
        Debug.WriteLine("Result: " + result);
    }
    catch (Exception ex)
    {
        Debug.WriteLine("Exception Message: " + ex.Message);
    }
    Debug.WriteLine("Task IsCanceled: " + theTask.IsCanceled);
    Debug.WriteLine("Task IsFaulted:  " + theTask.IsFaulted);
    if (theTask.Exception != null)
    {
        Debug.WriteLine("Task Exception Message: "
            + theTask.Exception.Message);
        Debug.WriteLine("Task Inner Exception Message: "
            + theTask.Exception.InnerException.Message);
    }
}

private async Task<string> DelayAsync()
{
    await Task.Delay(100);

    // Uncomment each of the following lines to
    // demonstrate exception handling.

    //throw new OperationCanceledException("canceled");
    //throw new Exception("Something happened.");
    return "Done";
}

// Output when no exception is thrown in the awaited method:
//   Result: Done
//   Task IsCanceled: False
//   Task IsFaulted:  False

// Output when an Exception is thrown in the awaited method:
//   Exception Message: Something happened.
//   Task IsCanceled: False
//   Task IsFaulted:  True
//   Task Exception Message: One or more errors occurred.
//   Task Inner Exception Message: Something happened.

// Output when a OperationCanceledException or TaskCanceledException
// is thrown in the awaited method:
//   Exception Message: canceled
//   Task IsCanceled: True
//   Task IsFaulted:  False



More information on the link https://docs.microsoft.com/pt-br/dotnet/csharp/language-reference/keywords/try-catch

Browser other questions tagged

You are not signed in. Login or sign up in order to post.