If or Try / Catch? [closed]

1

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

   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");
        }
    }
}
    
asked by anonymous 22.08.2017 / 17:04

0 answers