7024

Kotlin - Throw Custom Exception

Question:

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?

Answer1:

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.

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

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

and throw it like:

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

Answer2:

Like this:

class CustomException(message: String) : Exception(message) fun main(args: Array<String>) { throw CustomException("Error!") // >>> Exception in thread "main" } // >>> CustomException: Error!

<em>For more info: <a href="https://kotlinlang.org/docs/reference/exceptions.html" rel="nofollow">Exceptions</a></em>

Recommend

  • How to insert values from dynamically added rows into database
  • Double data-confirm prompt observed on rails with framework7
  • lateral cell space tableview Swift
  • Android How to call a method multiple times with a delay between them
  • How to set title name of the pdf. While viewing the Document(New Tab)
  • “RepeatForUnit” item missing in Calendar entry?
  • Customize jQuery submenu positioning
  • Django non-rel createsuperuser fails due to non-ascii characters
  • get all files in git diff in intellij
  • Can't access web service when connected to the network :: HTTP 407
  • Determine Active Panel
  • Is it possible to add a hyperlink to a UIAlertController?
  • Limit number of button clicks
  • in Gwt, there are 2 different packages (or 2 options) for doing drag n Drop? Which one is better?
  • Bulk loading into PostgreSQL from a remote client
  • When i select a Textfield the keyboard moves over it
  • Refresh JSF component after custom javascript Ajax call
  • Opening tel: links from UIWebView
  • How to search for a method or variable name within android project?
  • Smarter Removing Unnecessary WhiteSpace CSV
  • Why do you need 2 Javascript files for cross-platform Cordova plugin?
  • Keep rows with certain values always at the bottom while sorting in jquery tablesorter plugin
  • How to use AJAX to upload large CSV file? [closed]
  • Can I read another applications memory?
  • Typeahead.js does give me suggestions but doesn't select them
  • PHPMailer return to AJAX
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Comparing variables with strings bash
  • How to call jQuery function in HTML returned by AJAX
  • Terminal run dalvikvm with am.jar
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • `$http:badreq Bad Request Configuration` - from angular post method, what is wrong here?
  • How to warp text around image in iOS?
  • How to handle div that is created dynamically in a table
  • Google App Engine backend servlet not responding
  • How to get rgb from transparent pixel in js
  • Make checkout phone field optional for specific countries in WooCommerce
  • Call Microservice from another Microservice within Docker
  • Write to .csv file with PHP (Commas in Data Error)
  • ReferenceError: TextEncoder is not defined