简体   繁体   English

为什么getMessage()在Kotlin中是一个未解析的引用,带有Exception类?

[英]Why is getMessage() an unresolved reference in Kotlin with an Exception class?

Kotin documentation says that "All exception classes in Kotlin are descendants of the class Throwable. Every exception has a message, stack trace and an optional cause." Kotin文档说 “Kotlin中的所有异常类都是Throwable类的后代。每个异常都有消息,堆栈跟踪和可选原因。”

The Java documentation for Throwable shows a getMessage() method. Throwable的Java文档显示了一个getMessage()方法。 But the Kotlin documentation for Throwable does not have a getMessage(). 但是ThrowableKotlin文档没有getMessage()。 So this code: 所以这段代码:

fun main(args: Array<String>)
{
   try
   {
      println("args size: ${args.size}");
   }
   catch (e: Exception)
   {
      println(e.getMessage())
      System.exit(1)
   }
}

gives me this compile error: 给我这个编译错误:

test_exception.kt:12:17: error: unresolved reference: getMessage
      println(e.getMessage())
                ^

suggesting that I am using a Kotlin Exception class derived from a Kotlin Throwable class. 建议我使用从Kotlin Throwable类派生的Kotlin Exception类。

However, if I change getMessage() to toString() and add a throw: 但是,如果我将getMessage()更改为toString()并添加throw:

fun main(args: Array<String>)
{
   try
   {
      println("args size: ${args.size}");
      throw Exception("something went wrong")
   }
   catch (e: Exception)
   {
      println(e.toString())
      System.exit(1)
   }
}

I get this message: 我收到这条消息:

java.lang.Exception: something went wrong

Which seems to say that the Exception class is NOT a Kotlin Exception class - but the java version which has a getMessage() method and I shouldn't get a compile error when I try to use it. 这似乎说Exception类不是Kotlin Exception类 - 但是java版本有一个getMessage()方法,当我尝试使用它时我不应该得到编译错误。

There is no other Throwable except for java.lang.Throwable . 除了java.lang.Throwable之外,没有其他Throwable This class is used both by Java and by Kotlin programs. Java和Kotlin程序都使用此类。

The fact that Throwable has an entry in the Kotlin documentation suggests that this is a special compiler "alias". Throwable在Kotlin文档中有一个条目这一事实表明这是一个特殊的编译器“别名”。 That means that for all intents and purposes it is a Kotlin class, instances of which are represented by java.lang.Throwable . 这意味着,对于所有意图和目的,它是一个Kotlin类,其实例由java.lang.Throwable表示。 Black magic. 黑魔法。

TL;DR: TL; DR:

The equivalent of e.getMessage() in Kotlin is e.message , which is described in the docs as open val message: String? 在Kotlin中等效的e.getMessage()e.message ,在文档中将其描述为open val message: String? .

Since Throwable is not used directly from Java, but mapped to Kotlin, you cannot use the Java notation e.getMessage() for the property. 由于Throwable不是直接从Java使用,而是映射到Kotlin,因此不能对该属性使用Java表示法e.getMessage() Here is more about mapped types: http://kotlinlang.org/docs/reference/java-interop.html#mapped-types 以下是有关映射类型的更多信息: http//kotlinlang.org/docs/reference/java-interop.html#mapped-types

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM