-
Notifications
You must be signed in to change notification settings - Fork 0
/
02exceptionExample.kt
68 lines (47 loc) · 2.3 KB
/
02exceptionExample.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import kotlin.IllegalArgumentException
/*
In Java, all exceptions that are not of type RuntimeException have to be declared as checked exceptions
You have to make sure the catch any exception that might be thrown and have to annotate the function with the "throws" keyword
Kotlin gets rid of checked exceptions.
They give a simple example on why they removed it:
https://kotlinlang.org/docs/reference/exceptions.html
The following is an example interface of the JDK implemented by StringBuilder class:
Appendable append(CharSequence csq) throws IOException;
every time we need to append something in the string builder, we need to wrap it with a try catch.
If we use a String builder for building log strings, the catch block will be empty.
For good Java practices you should not ignore exceptions and leave the catch block empty
try {
log.append(message)
}
catch (IOException e) {
// Must be safe
}
*/
fun main(args: Array<String>) {
// Kotlin does have the standard try catch finally syntax, which is known from java.
// In Kotlin try can be put in an expression to retrieve a return value
val n = try { failWith { IllegalArgumentException("This failed") } } catch (e: IllegalArgumentException) {e.message}
println(n)
// throw is also an expression that can be contained in the elvis operator.
// Elvis operator will be explained in the null safety part
val a = A(null)
val result =
try { a.message ?: throw IllegalArgumentException("Message was null") }
catch(e: IllegalArgumentException) {
println("The last line in the expression automatically uses the return keyword")
"This string will be marked as unused by the IDE"
"This string will be returned"
}
println(result)
}
// Class only uses a constructor and therefore
class A(val message: String?)
/*
Nothing is a special type in kotlin with no values that indicates that a function will never return
The difference between Nothing and Unit is for indication purposes.
Unit -> is meant to be the Java void keyword and show that a function has no value of interest that will be returned
Nothing -> Indicates that a function will not return normally such as a function that will always throw an exception
*/
fun failWith(context: () -> Throwable): Nothing {
throw context()
}