1

If something happens in my app I want to kill the app there and then. In swift I can use fatalError(), is there a java equivalent?

Lewis Black
  • 957
  • 2
  • 8
  • 22

2 Answers2

1

I don't there's any method equivalent to fatalError in Android.

If you want to kill the entire process then you can use android.os.Process.killProcess(android.os.Process.myPid());.

To mimic the same behaviour of fatalError you'll probably have to implement it yourself.

The behaviour you'll need to add besides killing the process is

  • to log a message, you can to this by using Log
  • print the file and line number, for this you can follow this thread on SO
Community
  • 1
  • 1
Radu Diță
  • 13,476
  • 2
  • 30
  • 34
0

Kotlin's Preconditions.kt file includes an error(message: Any) function that, in turn, throws an IllegalStateException[1].

As long as you're not swallowing the exception, it should kill the app much like fatalError().

[1] Source code

Nate Whittaker
  • 1,866
  • 15
  • 14