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

255
Visualizações
kotlin singleton exception is bad or good?

i'm studying kotlin. I have a question about using exception. which is better to use object exception or class exception in kotlin?

object CustomDuplicateId : RuntimeException("Invalid user with Id")
class CustomDuplicateId : RuntimeException("Invalid user with Id")

if im using this exception only one location, than stacktrace is always same. so i think... it doesn't need to use class. is it right?

or is it bad to use singleton exception?

can anyone let me know what's better code to write exception?

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

0

There are multiple reasons why it's incorrect to use object.

  1. the stacktrace is wrong in several ways:

    • It's created at instantiation time, so you'll see <clinit> as first line in the stacktrace because it's created when initializing the object's class for the first time
    • Even if you throw it from the same place, it won't necessarily be when called from the same caller, so the bottom (root) of the stacktrace will be incorrect if this exception is thrown multiple times across the life of your application
  2. technically the exception class has mutable parts and it would be dangerous/incorrect to allow user code to tamper with this. A very concrete example of this is that user code could add suppressed exceptions to your exception instance (which would only be valid for one throw and yet persist in your object). This is technically a memory leak.

  3. it's likely you would need different messages with more information about why it was thrown (in that case the duplicate ID is very much needed) - so you need different instances with different data anyway

  4. you might throw it from different places in the future (even now in tests/mocks maybe?), and in that case the stacktrace would be even more wrong

  5. independently of technicalities like the above, class vs object also sends a message to the reader that is a bit unclear - why an object here? This exception is not inherently unique. It just so happens that you throw it in one specific place right now and rely on the stacktrace being the same.

Here is an example for the major issue (#1):

object MyExceptionObject : RuntimeException("boom")

fun main() {
    try {
        caller1() // line 7
    } catch (e: Exception) {
    }
    caller2() // line 10
}

fun caller1() = doStuff() // line 13
fun caller2() = doStuff() // line 14

fun doStuff() {
    throw MyExceptionObject // line 17
}

The caller1() throws the exception but that one is caught. The caller2() throws the exception again and that one is not caught. The stacktrace we get for the uncaught exception incorrectly shows the caller 1 (with lines 7 and 13) but the correct one should be caller 2 (with lines 10 and 14):

Exception in thread "main" com.example.MyExceptionObject: boom
    at com.example.MyExceptionObject.<clinit>(ExceptionObjectExample.kt)
    at com.example.ExceptionObjectExampleKt.doStuff(ExceptionObjectExample.kt:17)
    at com.example.ExceptionObjectExampleKt.caller1(ExceptionObjectExample.kt:13)
    at com.example.ExceptionObjectExampleKt.main(ExceptionObjectExample.kt:7)
    at com.example.ExceptionObjectExampleKt.main(ExceptionObjectExample.kt)

All in all, just use a class.

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