我想知道为什么在下面的代码中没有捕捉到异常:
private inline fun <reified T : Any> parseResponse(
httpConnection: HttpURLConnection,
noinline callback: ResponseBlock<T>?
) {
with(httpConnection) {
var error: Throwable? = null
val model = try {
if (this.responseCode in 200..299) {
jacksonObjectMapper().readValue(
this.inputStream.bufferedReader().readText(),
T::class.java
)
} else {
null
}
} catch (e: Exception) {
error = e
null
}
if (model == null && error == null) {
error = try {
if (this.responseCode !in 200..299) {
val errorResponse = this.errorStream.bufferedReader().readText()
Throwable(errorResponse)
} else {
null
}
} catch (e: Exception) {
e
}
}
error?.let {
Log.e(this::class.simpleName, it.message, it)
}
callback?.invoke(this.responseCode, model, error)
this.disconnect()
}
}
函数的调用方式如下:
private inline fun <reified T : Any> get(
pathComponents: Array<String>,
noinline callback: ResponseBlock<T>?,
queryParameters: Map<String, Any>? = null
) {
thread(start = true) {
val urlRequest = createUrlRequest(HttpMethod.GET, pathComponents, queryParameters)
parseResponse(urlRequest, callback)
}
}
但我还是遇到了一个导致应用程序崩溃的异常:
E/HttpURLConnectionImpl: Failed to connect to /10.0.2.2:8080
java.net.ConnectException: Failed to connect to /10.0.2.2:8080
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:147)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
at io.openremote.app.network.ApiManager$getAppConfig$$inlined$get$1.invoke(ApiManager.kt:148)
at io.openremote.app.network.ApiManager$getAppConfig$$inlined$get$1.invoke(ApiManager.kt:14)
at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)
E/AndroidRuntime: FATAL EXCEPTION: Thread-2
Process: io.openremote.app, PID: 3901
java.net.ConnectException: Failed to connect to /10.0.2.2:8080
at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:147)
at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:116)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:186)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:542)
at io.openremote.app.network.ApiManager$getAppConfig$$inlined$get$1.invoke(ApiManager.kt:148)
at io.openremote.app.network.ApiManager$getAppConfig$$inlined$get$1.invoke(ApiManager.kt:14)
at kotlin.concurrent.ThreadsKt$thread$thread$1.run(Thread.kt:30)
我想说try/catch块会捕获异常,但唉。。。
什么原因导致异常没有被捕获?我想说线程并不重要,因为我使用try/get块来处理线程中的异常。
在Laalto的回答之后,我更新了代码,如下所示(对于那些感兴趣的人):
private inline fun <reified T : Any> parseResponse(
httpConnection: HttpURLConnection,
noinline callback: ResponseBlock<T>?
) {
with(httpConnection) {
val parsedResult = try {
if (this.responseCode in 200..299) {
Triple(
this.responseCode, jacksonObjectMapper().readValue(
this.inputStream.bufferedReader().readText(),
T::class.java
), null
)
} else {
val errorResponse = this.errorStream.bufferedReader().readText()
Triple(this.responseCode, null, Throwable(errorResponse))
}
} catch (e: Exception) {
Triple(0, null, e)
}
parsedResult.third?.let {
Log.e(this::class.simpleName, it.message, it)
}
callback?.invoke(parsedResult.first, parsedResult.second, parsedResult.third)
this.disconnect()
}
}
堆栈跟踪显示了一个试图调用get响应代码()
的异常。在结尾没有try
-get
块的情况下,可以访问响应代码
:
callback?.invoke(this.responseCode, model, error)
早期的捕获块工作得很好,但是代码继续执行这部分,然后抛出异常。
正如在更新的问题中一样,您已经围绕这个问题重新构建了代码。
代码: 崩溃仍然被抛出。见下文: 2019-02-20 19:45:22.514 22435-22435/com.xxx。xxx E/PedalApp:无法保存层java.lang.RuntimeException:停止失败。在android.media.MediaRecorder。在com.androidchekhov.looperpedal.PedalActivity$onCreate$6.i
我试图用OkHttp和Cucumber在静态编程语言中设置一个Spring启动项目,并且在运行Cucumber任务时遇到以下错误。如何修复? 还有build gradle kts片段 我看到了这个错误https://github.com/square/okio/issues/647看起来可能是它,并修复了这个build.gradle,我如何将其翻译为kotlinbuild.gradle.kts?
我有一个Spring Boot应用程序,以Kotlin为语言,Gradle为构建系统。所以基本上,我试图用应用程序源代码和依赖项构建一个胖jar,可以使用Java命令行工具运行。 Gradle构建脚本: 如果我使用gradle命令运行项目,那么它运行正常。但是当我构建jar并尝试ti运行时,它会抱怨以下错误:- 应用.kt:- 不知道我到底哪里做错了什么。
在Java中,程序员可以为JUnit测试用例指定预期的异常,如下所示: 在科特林我该怎么做?我尝试了两种语法变体,但都不起作用:
我在表中的列上添加了唯一约束。当违反约束时,它会抛出一个我无法捕获并传达给用户的异常。 <代码>公开:事务尝试#0失败:java。sql。BatchUpdateException:Batch entry 0 INSERT INTO templates(created\u at,is\u deleted,name,sections)值('2018-10-03 16:31:25.732 05:30',
如图所示,https://stackoverflow.com/a/16639438/8949356,在Java中,当声明的类是公共类时,可以重写其函数 但是我想知道如何用静态编程语言编写完全相同的代码,我已经尝试了很多,但没有找到任何关于这个主题的东西。我可以在Java中去做这件事,但我的其余代码是用静态编程语言编写的,而且我不能一直带着这种怀疑;静态编程语言对我来说是一个很好的工具,我想学习它。