try / catch does not show correct message

3

Staff have a my 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));

In the try / catch, the ex.Message, instead of showing 'URL' xxx 'is not valid ", is showing the message" Message an exception was thrown by the target of an invocation. "

Try / catch block

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

Does anyone know why ex.Message comes different?

    
asked by anonymous 13.08.2014 / 20:24

3 answers

6

I discovered the problem / solution.

As the project is composed of several DLLs, when calling one of these DLLs, the error presented by ex.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 inside the DLL that was called.

For this I used the InnerException property, which is the exception error message generated inside the target.

The font looks 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 { }
}
    
14.08.2014 / 16:45
0

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

    
13.08.2014 / 21:02
0

Simulating your example Marlon Tiedt, I noticed that it worked:

Note: I've raised the error by typing 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 { }
}

    
14.08.2014 / 15:35