Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

318
Vistas
¿Cómo obtener el nombre de la variable en el bloque catch?

Aquí tengo varias variables de string asignadas, pero quiero detectar el error en qué variable y su nombre.

 try { sSrNo = Convert.ToString(valueArrayCustomerDetails[row, 1]); sNotice_No = (string)valueArrayCustomerDetails[row, 2]; dt_notice_date = Convert.ToDateTime(valueArrayCustomerDetails[row, 3]); sAgreement_No = Convert.ToString(valueArrayCustomerDetails[row, 4]); sBorrower = (string)valueArrayCustomerDetails[row, 5]; } catch (Exception ex) { MessageBox.Show(ex.ToString() + "\n Error At :- Row-" + row + " Column-"); // here i want variable name at which error occurs throw; }
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Bueno, tienes que usar múltiples intentos de captura si quieres esa información:

 try { sSrNo = Convert.ToString(valueArrayCustomerDetails[row, 1]); } catch (Exception ex) { MessageBox.Show(ex.ToString() + "\n Error At :- Row-" + row + " 1"); throw; } try { sNotice_No = (string)valueArrayCustomerDetails[row, 2]; } catch (Exception ex) { MessageBox.Show(ex.ToString() + "\n Error At :- Row-" + row + " 2"); throw; } // ...

Otra opción es crear un método auxiliar como este:

 public static class Utils { public static T TryDo<T>(Func<T> returnMethod, Action<Exception> exceptionAction = null, bool throwOnException = true, T fallbackValue = default(T)) { try { return returnMethod(); }catch(Exception ex) { exceptionAction(ex); if(throwOnException) { throw; } } return fallbackValue; } }

Ahora puede usar este código conciso y legible:

 sSrNo = Utils.TryDo(() => Convert.ToString(valueArrayCustomerDetails[row, 1]), ex => MessageBox.Show(GetMessage(ex, row, 1))); sNotice_No = Utils.TryDo(() => Convert.ToString(valueArrayCustomerDetails[row, 2]), ex => MessageBox.Show(GetMessage(ex, row, 2))); dt_notice_date = Utils.TryDo(() => Convert.ToDateTime(valueArrayCustomerDetails[row, 3]), ex => MessageBox.Show(GetMessage(ex, row, 3))); sAgreement_No = Utils.TryDo(() => Convert.ToString(valueArrayCustomerDetails[row, 4]), ex => MessageBox.Show(GetMessage(ex, row, 4))); sBorrower = Utils.TryDo(() => Convert.ToString(valueArrayCustomerDetails[row, 5]), ex => MessageBox.Show(GetMessage(ex, row, 5))); // local method string GetMessage(Exception ex, int row, int col) => $"{ex.ToString()} Error At :- Row-{row} Column-{col}";
over 4 years ago · Santiago Trujillo Denunciar

0

string index="1"; try { sSrNo = Convert.ToString(valueArrayCustomerDetails[row, 1]); index="2"; sNotice_No = (string)valueArrayCustomerDetails[row, 2]; index="3"; dt_notice_date = Convert.ToDateTime(valueArrayCustomerDetails[row, 3]); index="4"; sAgreement_No = Convert.ToString(valueArrayCustomerDetails[row, 4]); index="5"; sBorrower = (string)valueArrayCustomerDetails[row, 5]; } catch (Exception ex) { MessageBox.Show(ex.ToString() + "\n Error At :- Row-" + row + " Column-"+index); // here i want variable name at which error occurs throw; }
over 4 years ago · Santiago Trujillo Denunciar

0

Intenta usar esto:

Dará el número de línea en el que se lanza la excepción, obteniendo así la variable.

 catch(Exception ex) { var LineNumber = new StackTrace(ex, true).GetFrame(0).GetFileLineNumber(); MessageBox.Show(ex.StackTrace + "\n Error at :- Row-" + LineNumber); }

ex.StackTrace da un mensaje de error detallado.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda