Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use case for RuntimeException from package Kotlin

I was writing some code and was going to throw a RuntimeException as a default for something, when I noticed that there are two options for RuntimeException - one from java.lang and one from kotlin: enter image description here

Inside the kotlin version of this, there's an entire list of other methods which work the same way :

enter image description here

So, from my understanding of this, the one from package kotlin is simply an alias for the java equivalent (correct me if I'm wrong) which leads me to the question :

what is the point of having this alias file and when should you use it over the "standard" java equivalent ? Does this simply save a few imports ?

like image 981
a_local_nobody Avatar asked Jan 25 '23 18:01

a_local_nobody


2 Answers

When using the JDK, these map to JDK classes. When using Kotlin for Javascript, they would map to a specific implementation in the Kotlin Javascript library. Documentation about actual and expect here.

To answer your question, if there's a chance of you porting your code to also work on another platform, always use the kotlin. variant. Otherwise, it doesn't matter.

like image 156
Tenfour04 Avatar answered Jan 30 '23 03:01

Tenfour04


In my opinion typealias is strong feature provided by Kotlin language.

Why? Because it gives ability to extend code to multiple domains like extension to provide interoperability between any other languages and Kotlin.

It also becomes helpful when providing APIs or SDK with semantic versioning without worrying much about changes affecting on lower versions of APIs.

One good example would be Collections derived from Java to Kotlin language with some additional & powerful methods as typealias (Literally it saves lot of development time and efforts).

Another good example would be multiplatform programming support by Kotlin language that helps you create APIs with actual and expect keywords implementation.

So long story short, I prefer using RuntimeException from Kotlin package to extend support amongst variety of Kotlin versions (You can see newly added classes starting from version 1.3, but doesn't affect existing API).

like image 35
Jeel Vankhede Avatar answered Jan 30 '23 04:01

Jeel Vankhede