浏览代码

Catch exceptions

Him188 6 年之前
父节点
当前提交
d6c9b9359f
共有 1 个文件被更改,包括 6 次插入3 次删除
  1. 6 3
      mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/Main.kt

+ 6 - 3
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/Main.kt

@@ -72,9 +72,12 @@ suspend fun main() {
                     val image: Image by message
                     // 等同于 val image = message[Image]
 
-                    reply(image + " downloading")
-                    image.downloadTo(newTestTempFile(suffix = ".png").also { reply("Temp file: ${it.absolutePath}") })
-                    reply(image.id.value + " downloaded")
+                    try {
+                        image.downloadTo(newTestTempFile(suffix = ".png").also { reply("Temp file: ${it.absolutePath}") })
+                        reply(image.id.value + " downloaded")
+                    } catch (e: Exception) {
+                        reply(e.message ?: e::class.java.simpleName)
+                    }
                 }
             }
         }