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

505
Visualizações
What's the equivalent of Swift's fatalError() in Kotlin

Say I want to do sth like

val foo = when(bar) {
   "one" -> true
   "two" -> false
   else -> // in Swift it would be `fatalError()`
}

How do I do it?

I'm aware that there are generally better approaches to this kind of problem, but I want to know how to easily induce a crash in Kotlin too :)

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

0

You could throw an exception. This will bubble up the call stack until somebody handles it or you run out of stack frames and the application crashes.

val foo = when(bar) {
   "one" -> true
   "two" -> false
   else -> throw IllegalArgumentException("$bar is Unknown")
}

I just used IllegalArgumentException here, but you might want to define your own.

over 4 years ago · Santiago Trujillo Relatório

0

If you look at the TODO() global function in Kotlin:

public inline fun TODO(): Nothing = throw NotImplementedError()

You can see that it is a function that returns Nothing (similar to Swift'sNever), and it throws a NotImplementedError.

With this, we can create our own fatalError function to mimic Swift:

public fun fatalError(message: String = ""): Nothing = throw NotImplementedError(message)

And because it returns Nothing you can use it the same way you would in Swift:

fun getValue(): Boolean = when(this) {
  "true" -> true
  "false" -> false
  else -> fatalError("Cannot determine value for $this")
}
over 4 years ago · Santiago Trujillo Relatório

0

Throwing an exception (or, better still, an Error) is one way, but it may not shut down the current thread: any of the calling functions could catch the exception, or an UncaughtExceptionHandler could have been set for the Thread (or its ThreadGroup).

Nor will it shut down any other threads that may be running.  (Even if you haven't started any yourself, they're used by coroutines, GUI toolkits, and other execution frameworks.)

To shut down the whole app, you should use the Kotlin exitProcess() function, or in Kotlin/JVM the equivalent System.exit() method.  That will terminate all threads.

But even that will do a ‘graceful’ shutdown, running any shutdown hooks, finalizers, &c.  If you need to forcibly kill a Kotlin/JVM app immediately, call Runtime.getRuntime().halt().

(And of course, it's probably good practice to first display something to stdout or the logging framework you're using, to indicate what the problem was.)

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