public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
To run it on an android device:
javac HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
To run it on an android device:
javac HelloWorld.java
fun getUser(): User? { | |
// ... | |
} | |
val user: User? = getUser() | |
if (user != null) { | |
println("Hello, ${user.name}") | |
} else { | |
throw IllegalStateException("User not found!") | |
} |
val user: User = getUser() ?: throw IllegalStateException("User not found!") | |
println("Hello, ${user.name}") |
fun fail(message: String) { | |
throw IllegalStateException(message) | |
} | |
…. | |
val user: User = getUser() ?: fail("User not found!") | |
println("Hello, ${user.name}") |
fun fail(message: String) { | |
throw IllegalStateException(message) | |
} | |
// Is the same as: | |
fun fail(message: String): Unit { | |
throw IllegalStateException(message) | |
} |
fun fail(message: String): Nothing { | |
throw IllegalStateException(message) | |
} | |
// ... | |
val user: User = getUser() ?: fail("User not found!") | |
println("Hello, ${user.name}") |
fun fail(message: String): Nothing { | |
val throwable = Throwable(message) | |
Thread.setDefaultUncaughtExceptionHandler { t, e -> System.err.println(e.message) } | |
throw throwable | |
} | |
// ... | |
val user: User = getUser() ?: fail("User not found!") | |
println("Hello, ${user.name}") |
// Java | |
@FunctionaInterface | |
public interface UncaughtExceptionHandler { | |
void uncaughtException(Thread t, Throwable e); | |
} |
Thread.setDefaultUncaughtExceptionHandler ({ t, e -> //… }) |