Return inside try ... catch does not work

7

I made this method:

public int VerificaUltimaAnalise()
        {
            //Desenvolvimento
            WFExecutor vcmpExecutor = null;
            WFAnalise vcmpAnalise = null;
            Core vmpaCore = null;
            int vintCdTransacao = 0;
            int vintCdProxProcesso = 0;
            int vintCdAnalise = 0;

            try
            {
                //Instâncias e Inicializalções
                vcmpExecutor = new WFExecutor();
                vcmpAnalise = new WFAnalise();
                vmpaCore = (Core)Page.Master;

                //Desenvolvimento
                //vintCdTransacao = vcmpExecutor.ConsultarTransacao(Request.Path.Substring(Request.Path.LastIndexOf("/") + 1));

                vintCdProxProcesso = vcmpAnalise.ProximoProcessoAnalise(vintCdTransacao, int.Parse(hdfCdUsuario.Value), ref vintCdAnalise);

                return vintCdProxProcesso;
            }
            catch (Exception Ex)
            {
                Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
                Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
            }
        }

And in design still, give me this error:

  

Not all return paths return a value

This always happens when I try to return inside a try ... catch block. What should I do to resolve this?

    
asked by anonymous 23.01.2015 / 14:03

5 answers

10

The problem is the lack of a return . You should do this in catch also, so if there is an exception a return will always be executed. In the current form the return will only be executed if no failure occurs. This is inconsistent. According to the method signature it should always return something, even if a failure occurs.

The translation of the error message already illustrates the problem "not all paths return a value". Remember that try-catch is a flow control, it deflects code execution, it creates different execution paths according to the events.

You probably want to return nothing like this:

public int VerificaUltimaAnalise()
    {
        //Desenvolvimento
        WFExecutor vcmpExecutor = null;
        WFAnalise vcmpAnalise = null;
        Core vmpaCore = null;
        int vintCdTransacao = 0;
        int vintCdProxProcesso = 0;
        int vintCdAnalise = 0;

        try
        {
            //Instâncias e Inicializalções
            vcmpExecutor = new WFExecutor();
            vcmpAnalise = new WFAnalise();
            vmpaCore = (Core)Page.Master;

            //Desenvolvimento
            //vintCdTransacao = vcmpExecutor.ConsultarTransacao(Request.Path.Substring(Request.Path.LastIndexOf("/") + 1));

            vintCdProxProcesso = vcmpAnalise.ProximoProcessoAnalise(vintCdTransacao, int.Parse(hdfCdUsuario.Value), ref vintCdAnalise);

            return vintCdProxProcesso;
        }
        catch (Exception Ex)
        {
            Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
            Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
            return 0;
        }
    }

Just remembering that capturing Exception is usually not a good idea in most cases.

Suggested reading .

    
23.01.2015 / 14:10
5

This happens because the code can never actually reach return . Using your own code, for example:

        try
        {
            //Instâncias e Inicializalções
            vcmpExecutor = new WFExecutor();
            vcmpAnalise = new WFAnalise();
            vmpaCore = (Core)Page.Master;

            // Suponha que ocorra uma exceção na linha abaixo:
            vintCdProxProcesso = vcmpAnalise.ProximoProcessoAnalise(vintCdTransacao, int.Parse(hdfCdUsuario.Value), ref vintCdAnalise);

            return vintCdProxProcesso;
        }

I commented on a line in your code. Note that if an exception occurs on this line, the program goes straight to catch , and its catch has no return instruction of anything:

        catch (Exception Ex)
        {
            Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
            Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
        }

There are two ways to solve:

1. Putting return in try and other in catch

For example:

    public int VerificaUltimaAnalise()
    {
        //Desenvolvimento
        WFExecutor vcmpExecutor = null;
        WFAnalise vcmpAnalise = null;
        Core vmpaCore = null;
        int vintCdTransacao = 0;
        int vintCdProxProcesso = 0;
        int vintCdAnalise = 0;

        try
        {
            //Instâncias e Inicializalções
            vcmpExecutor = new WFExecutor();
            vcmpAnalise = new WFAnalise();
            vmpaCore = (Core)Page.Master;

            //Desenvolvimento
            //vintCdTransacao = vcmpExecutor.ConsultarTransacao(Request.Path.Substring(Request.Path.LastIndexOf("/") + 1));

            vintCdProxProcesso = vcmpAnalise.ProximoProcessoAnalise(vintCdTransacao, int.Parse(hdfCdUsuario.Value), ref vintCdAnalise);

            return vintCdProxProcesso;
        }
        catch (Exception Ex)
        {
            Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
            Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
            return 0;
        }
    }

2. Placing a return in the last line of execution of the function or method

    public int VerificaUltimaAnalise()
    {
        //Desenvolvimento
        WFExecutor vcmpExecutor = null;
        WFAnalise vcmpAnalise = null;
        Core vmpaCore = null;
        int vintCdTransacao = 0;
        int vintCdProxProcesso = 0;
        int vintCdAnalise = 0;

        try
        {
            //Instâncias e Inicializalções
            vcmpExecutor = new WFExecutor();
            vcmpAnalise = new WFAnalise();
            vmpaCore = (Core)Page.Master;

            //Desenvolvimento
            //vintCdTransacao = vcmpExecutor.ConsultarTransacao(Request.Path.Substring(Request.Path.LastIndexOf("/") + 1));

            vintCdProxProcesso = vcmpAnalise.ProximoProcessoAnalise(vintCdTransacao, int.Parse(hdfCdUsuario.Value), ref vintCdAnalise);

            return vintCdProxProcesso;
        }
        catch (Exception Ex)
        {
            Mensagem = (wucMensagens)Page.Master.FindControl("wucMasterMensagens");
            Mensagem.ExibirMensagem(wucMensagens.TipoAlerta.Erro, Ex.Source, Ex.Message, Ex.StackTrace);
        }

        return 0;
    }
    
23.01.2015 / 14:10
3

Without going into the merits of the pattern you chose to handle exceptions ...

You cleared the exception in catch , so the method will always run successfully.

Because it is a function, it requires a return. To resolve the compilation error, include a return within catch returning a default value.

    
23.01.2015 / 14:08
-1

Insert a block Finally:

Finally
{
 return vintCdProxProcesso;
}

As the Finally, the code will always go through this stretch, in case of success or failure, as your variable was initialized with zero, then you will get the return int expected by the method.

I hope I have helped !!!

    
23.01.2015 / 17:51
-3

Place a

finaly{
   return retorno
}

This always returns something even though it is null .

    
23.01.2015 / 20:17