Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

146
Visualizações
Is it okay to throw different exceptions for the exact same problem if it occurs in a different context?

I have a simple problem in my project (C# based Windows Service) which I just cannot wrap my head around.

In several classes of my application I need to load an interval (time span), from the application configuration. In the configuration file this timespan is stored as a string, such as "1d8h". Obviously, the application needs to parse this string before it can use the time span. In one case, I am passing this string as an argument to a constructor while initializing several objects which all have their own time span. In the constructor I am then parsing this string and throwing an ArgumentException if the parsing fails for whatever reason.

Somewhere else in my application I need to do the exact same thing, but this time in a static constructor which is not allowed to have any arguments. In this case, I am throwing a System.Configuration.ConfigurationErrorsException because I am not working with any arguments so throwing an ArgumentException would not be correct in this case.

Is it perfectly fine to throw different exceptions in both cases, despite them being caused by the exact same operation? If not, what should I do instead?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I suggest to use FormatException in both cases.

As you said, ArgumentException signals a problem with an argument, usually its type or kind (while ArgumentNullException is used specifically for null arguments). Therefore, neither is acceptable in a static constructor accepting no arguments.

FormatException fits nicely. It signals a problem with the format of the argument itself, rather than its type or kind. It is thrown by most of C#'s native .Parse() methods, such as DateTime.Parse()

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda