Try/catch does not show correct message

Asked

Viewed 1,909 times

3

Guys, I have a C# code that has the code:

Uri resultadoURL;
bool resultado = Uri.TryCreate(Configuracoes.Configuracao.URL, UriKind.Absolute, out resultadoURL) && resultadoURL.Scheme == Uri.UriSchemeHttp;

if (!resultado)
    throw new Exception(String.Format("URL '{0}' não é válida!", Configuracoes.Configuracao.URL));

On Try/catch, the ex. Message, instead of showing "URL 'xxx' is not valid", is showing the message "Message an exception was triggered by the destination of a call".

Block of Try/catch

catch (Exception ex)
{
    try
    {
        Send(state.State, ToJson(new ResultadoDaIntegracao(false, ex.Message, null)));
    }
    catch { }
}

Does anyone know why the ex. Message comes different?

  • Wouldn’t your inner catch block be pq not propagating the Exception?

  • Why do you throw a Exception and not a more specific exception? And why do you have a try within a catch? And the latter catch is just an example, right?

3 answers

6


I found the problem/solution.

As the project is composed of several DLL’s, calling one of these DLL’s, the error presented by eg. Message is from the source process, so it shows the message "Message an exception was triggered by the destination of a call".

The error I needed to show was the error that occurred within the DLL that was called.

For this I used the Innerexception property, which is the Exception error message generated within the destination.

The source was like this:

catch (Exception ex)
{
    try
    {
        if (ex.InnerException != null)
            Send(state.State, ToJson(new ResultadoDaIntegracao(false, ex.InnerException.Message, null)));
        else
            Send(state.State, ToJson(new ResultadoDaIntegracao(false, ex.Message, null)));
    }
    catch { }
}

0

Simulating his example Marlon Tiedt, I found that it worked:

Note: I made the mistake by writing errorURL.

try
{
    Uri resultadoURL;
    bool resultado = Uri.TryCreate("errorURL", UriKind.Absolute, out resultadoURL) && resultadoURL.Scheme == Uri.UriSchemeHttp;
    if (!resultado)
    {
        throw new Exception(String.Format("URL '{0}' não é válida!", "errorURL"));
    }
}
catch (Exception ex)
{
    try
    {
        var e = ex;
    }
    catch { }
}

inserir a descrição da imagem aqui

  • I found the problem/solution. As the project is composed of several DLL’s, when calling one of these DLL’s, the error presented by ex.Message is from the calling process, and the error actually is within the Innerexception property, which is theoretically the original error message. thanks for the personal help.

  • 1

    @Cool, you can edit and add this to my answer if you like! or open your answer! Feel free. I believe the more enlightenment and congratulations ...!!!

0

Because you did not assign a value to it. There is the internal error (which has no specificity pq is standard). Try setting an Exception and the empty catch and pass the value of the message that returns in 'e' to 'ex'.

Browser other questions tagged

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