根据来源Closable.use,如果发生错误,就会抛出异常。
public inline fun <T : Closeable?, R> T.use(block: (T) -> R): R {
var exception: Throwable? = null
try {
return block(this)
} catch (e: Throwable) {
exception = e
throw e
} finally {
when {
apiVersionIsAtLeast(1, 1, 0) -> this.closeFinally(exception)
this == null -> {}
exception == null -> close()
else ->
try {
close()
} catch (closeException: Throwable) {
// cause.addSuppressed(closeException) // ignored here
}
}
}
Run Code Online (Sandbox Code Playgroud)
在 的大多数示例中Closable.use,不使用 try-catch,如下所示。为什么不需要错误处理?安全吗?
BufferedReader(FileReader("test.file")).use { …Run Code Online (Sandbox Code Playgroud) 当我开始学习Mockk进行测试时,我有以下问题。
Mockk官方示例显示如下:
val car = mockk<Car>()
Run Code Online (Sandbox Code Playgroud)
和
val car = mockkClass(Car::class)
Run Code Online (Sandbox Code Playgroud)
我觉得两者是一样的 有什么不同?
检测连接失败的最佳方法是什么?
我知道我们应该NetworkInfo.getState()用来获取连接状态,并且我也可以使用BroadcastReceiver NETWORK_STATE_CHANGED_ACTION来检测连接状态的变化。
我认为DISCONNECTED在这种情况下,在broadcastReceiver 中检测状态不匹配。
NetworkInfo.State.DISCONNECTED 表示仅“断开连接”,并不表示连接失败。