드를 정의하고, 후자를 해결하기 위해 종료 코드를 전달한다(오류를 처리하기 위해 제어 흐름
대신 데이터를 사용하는 예에 속한다). 오류를 출력하기 위해 확장 함수를 추출할 수도 있다.
예제
21.23
[
exceptions
-
to
-
values
.
8
:
src
/
main
/
java
/
travelator
/
marketing
/
HighValueCustomersMain
.
kt
]
fun main() {
val statusCode = using(
System.`in`.reader(),
System.out.writer(),
System.err.writer()
) { reader, writer, error ->
val errorLines = mutableListOf<ParseFailure>()
val reportLines = reader
.asLineSequence()
.toHighValueCustomerReport {
errorLines += it
}
if (errorLines.isEmpty()) {
reportLines.writeTo(writer)
0
} else {
errorLines.writeTo(error)
-1
}
}
exitProcess(statusCode)
}
inline fun <A : Closeable, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.