Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin协程和安全例外_Android_Kotlin_Retrofit2_Kotlinx.coroutines - Fatal编程技术网

Android Kotlin协程和安全例外

Android Kotlin协程和安全例外,android,kotlin,retrofit2,kotlinx.coroutines,Android,Kotlin,Retrofit2,Kotlinx.coroutines,我正在使用Reformation的协同路由适配器,我看到,如果由于URL不正确而导致网络调用失败,try/catch块将捕获它,但是如果我删除Internet权限以便获得SecurityException,则应用程序将崩溃 这是处理改装响应的代码: override suspend fun execute(): Result<IpAddress> = try { val result = dataRepository.getIpAddress().await() Re

我正在使用Reformation的协同路由适配器,我看到,如果由于URL不正确而导致网络调用失败,try/catch块将捕获它,但是如果我删除Internet权限以便获得SecurityException,则应用程序将崩溃

这是处理改装响应的代码:

override suspend fun execute(): Result<IpAddress> = try {
    val result = dataRepository.getIpAddress().await()
    Result.Success(result)
} catch (throwable: Throwable) {
    if (throwable is JobCancellationException) {
        throw throwable
    }
    Result.Error(throwable)
}

因为在一种情况下,改型会在内部捕获它,并将其作为错误传递回去,知道您可能希望处理它。在另一种情况下,它没有捕捉到它,并使应用程序崩溃。没有办法改变这一短叉改装,因为它发生在它们的内部线程上,只需将internet权限保留在那里。

但是try/catch不应该包装wait()吗调用捕获异常?否。线程A上的try catch不会捕获线程B上发生的异常。我认为它应该捕获异常,因为改型应该传播它。吞咽异常,包括
错误
s,对任何人都没有好处。我确实建议提交一个bug进行改进。您可以轻松地创建一个复制器,而无需任何协同程序。改型应该将所有异常传播到用户代码中,让用户决定如何处理它们。@RomanElizarov这不是一个bug。这是一个哲学上的选择。找不到主机并不是一个例外,这是一个预期的可能结果。用户可能传入了无效的url。这是一个使操作无法进行的编程错误。抛出异常是正确的做法。侧重点:不要使用
Result
作为
suspend fun
的返回类型。只需使用
IpAddress
。Result是一个密封的类,它允许我将Success和Error包装为Result类,这样客户端就可以处理调用的结果。除非您将整个代码库都提交给FP惯用法和高阶函数,否则抛出异常以表示错误将更为自然。更不用说在一个点上批量处理许多错误的麻烦要少得多。
override suspend fun getIpAddress(): Deferred<IpAddress> = dataService.getIpAddress()
interface DataService {
    @GET("/?format=json")
    fun getIpAddress(): Deferred<IpAddress>
}
09-09 12:27:25.467 12465 12495 E AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
09-09 12:27:25.467 12465 12495 E AndroidRuntime: Process: com.example.coroutines, PID: 12465
09-09 12:27:25.467 12465 12495 E AndroidRuntime: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:151)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at java.net.InetAddress.getAllByName(InetAddress.java:1154)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.Dns$1.lookup(Dns.java:40)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:214)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.RealCall$AsyncCall.execute(RealCall.java:147)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:764)
09-09 12:27:25.467 12465 12495 E AndroidRuntime: Caused by: android.system.GaiException: android_getaddrinfo failed: EAI_NODATA (No address associated with hostname)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at libcore.io.Linux.android_getaddrinfo(Native Method)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at libcore.io.BlockGuardOs.android_getaddrinfo(BlockGuardOs.java:172)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:137)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    ... 26 more
09-09 12:27:25.467 12465 12495 E AndroidRuntime: Caused by: android.system.ErrnoException: android_getaddrinfo failed: EACCES (Permission denied)
09-09 12:27:25.467 12465 12495 E AndroidRuntime:    ... 29 more