Kotlin-引发自定义异常 [英] Kotlin - Throw Custom Exception

查看:542
本文介绍了Kotlin-引发自定义异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Kotlin中引发自定义异常?我真的没有从文档中得到太多……

How can I throw a custom exception in Kotlin? I didn't really get that much off the docs...

在文档中,它描述了每个异常所需的内容,但是我如何实现它呢?

In the docs, it gets described what each exception needs, but how exactly do I implement it?

推荐答案

要记住的一件事:如果您使用的是IntelliJ IDE,只需复制/粘贴Java代码即可将其转换为Kotlin.

One thing to keep in mind: if you are using the IntelliJ IDE, just a simple copy/paste of Java code can convert it to Kotlin.

现在问您的问题.如果要创建自定义Exception,只需扩展Exception类,如:

Coming to your question, now. If you want to create a custom Exception, just extend Exception class like:

class TestException(message:String): Exception(message)

并像这样扔它:

throw TestException("Hey, I am testing it")

这篇关于Kotlin-引发自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆