If or Try/Catch?

Asked

Viewed 252 times

1

here at work I came across a code that deals error in an if instead of Try/Catch. The programmer justifies that the catch interrupts the code, but I believe that a Finally ensures the execution. Can you tell me what would be the best option for this case? Follow the example of the code:

   public async Task LoadView()
    {
        Exception error = null;




        try
        {

            IsBusy = true;

            var campos = await new NeutronAPIService().GetCamposAsync(Session.GetInstance().Usuario.DadosDeAcesso.Token, Janela.ID);

            if (campos != null)
            {
                Janela.Campos.Clear();

                foreach (var campo in campos)
                {
                    Janela.Campos.Add(campo);
                }
            }


            //var camposOrdenados = Janela.Campos.OrderBy<>
            var Content = new ContentPage();
            foreach (var campo in Janela.Campos)
            {
                var Entrytexto = new Entry { Placeholder = campo.Apelido, Keyboard = Keyboard.Text, };
                switch (campo.TipoCampo)
                {
                    case "TEXTO":
                        if (campo.ListaSelecao == null)
                        {
                            var LabelTexto = new Label { Text = campo.Apelido, };                                
                            MyStackLayout.Children.Add(LabelTexto);
                        }
                        else {

                            Picker picker = new Picker { Title=campo.Apelido, Margin = -7 };
                            var LabelPicker = new Label { Text = "Selecione: " };
                            foreach (var valor in campo.ListaSelecao.Valores)
                            {
                                picker.Items.Add(valor);
                            }
                            BoxView boxView = new BoxView
                            {
                                WidthRequest = 150,
                                HeightRequest = 150,
                            };
                            MyStackLayout = new StackLayout
                            {
                                Padding = -15,
                                Children = { picker, LabelPicker, boxView }
                            };
                            MainPage = new ContentPage
                            {Content = new StackLayout { Children = {MyStackLayout}} };
                        }
                        break;
                    case "DATA": 

                       //nao encontrei campo do tipo data 
                        var TextoData = new Label { Text = campo.Apelido, };
                        var DataInicio = new DatePicker
                        {
                            Margin = -7,                               
                            Format = "dd'/'MM'/'yyyy",
                        };
                        var DataFim = new DatePicker
                        {
                            Margin = -7,                               
                            Format = "dd'/'MM'/'yyyy",
                        };
                        var AgrupaDatas = new StackLayout
                        {
                            Children = { DataInicio, DataFim }
                        };
                        var DataInicioStl = new StackLayout
                        {
                            Padding = -15,
                            Children = { TextoData, AgrupaDatas }
                        };                           
                        MyStackLayout.Children.Add(DataInicioStl);
                        break;
                    case "NUMÉRICO":
                        if (campo.ListaSelecao == null)
                        {
                            if (campo.ValorInicial != null) {
                                var numericoVI = new Entry { Placeholder = campo.Apelido, Keyboard = Keyboard.Numeric };                                    
                                MyStackLayout.Children.Add(numericoVI);
                            }
                            if (campo.ValorFinal != null)
                            {
                                var numericoVF = new Entry { Placeholder = campo.ValorFinal, Keyboard = Keyboard.Numeric, BindingContext = campo.ID };                                   
                                MyStackLayout.Children.Add(numericoVF);
                            }
                        }
                        else
                        {
                            Picker picker = new Picker { Margin = -7 };
                            var LabelPicker = new Label { Text = "Selecione: " };
                            foreach (var valor in campo.ListaSelecao.Valores)
                            {
                                picker.Items.Add(valor);
                            }
                            BoxView boxView = new BoxView
                            {
                                WidthRequest = 150,
                                HeightRequest = 150,
                            };                                
                            MyStackLayout.Children.Add(picker);
                        }                            
                        break;
                    case "MEMO":
                        if (campo.ListaSelecao == null)
                        {
                            var EntryMemo = new Entry { Placeholder = campo.Apelido, Keyboard = Keyboard.Text, };                                
                            MyStackLayout.Children.Add(EntryMemo);
                        }
                        else
                        {
                            Picker picker = new Picker { Margin = -7 };
                            var LabelPicker = new Label { Text = "Selecione: " };
                            foreach (var valor in campo.ListaSelecao.Valores)
                            {
                                picker.Items.Add(valor);
                            }
                            BoxView boxView = new BoxView
                            {
                                WidthRequest = 150,
                                HeightRequest = 150,
                            };                                
                            MyStackLayout.Children.Add(picker);
                        }                           
                        break;
                }

            }
        }
        catch (Exception ex)
        {

        }
        finally
        {
            IsBusy = false;
        }

        if (error != null)
        {
            await Xamarin.Forms.Application.Current.MainPage.DisplayAlert(
             "Erro no pesquisa view Model!", error.Message, "OK");
        }
    }
}
  • 1

    We have no way of knowing, you need too much detail, more code, more context. Surely this exception capture is wrong, but we can’t help any more without seeing the rest of the code. Show at least the Try, if possible further. This if is another code or is it below the try-ctyach-finally?

  • Hello! I edited the question by placing the whole code snippet. I look forward to hearing about how best to deal with this error. Thanks

  • 2

    What error do you want to handle? This is the most important to know when you will catch an exception. Then you need to know why you want to capture the exception. Finally, what can you do when you capture the exception.There is another way to deal with this without being by exception?

No answers

Browser other questions tagged

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