title: How to extend a class that has multiple constructors in Kotlin?
body: <p>I am in the processing of learning Kotlin and ran into a problem I couldn't figure out.
  I would like to extend the Java class <code>RuntimeException</code> in Kotlin and be able to use any one of three of its constructors, in different circumstances (based on what info I have at the time I want to throw an exception). In java my class would look like this:</p>
  <pre><code>public class PhotoLibException extends RuntimeException {
  
      public PhotoLibException(String message, RuntimeException ex) {
          super(message, ex);
      }
  
      public PhotoLibException(String message) {
          super(message);
      }
  
      public PhotoLibException(RuntimeException ex) {
          super(ex);
      }
  }
  </code></pre>
  <p>When I try to do this in Kotlin, I used this answer as a guide: <a href="https://stackoverflow.com/questions/19299525/kotlin-secondary-constructor">Kotlin secondary constructor</a> however, I had a problem trying to figure out how to invoke the appropriate super constructor correctly. For example, using functions seemed to be a good approach, like this:</p>
  <pre><code>fun PhotoLibException(message: String): PhotoLibException {
      val ex = null
      return PhotoLibException(message, ex)
  }
  
  fun PhotoLibException(ex: Exception): PhotoLibException {
      val message = &quot;&quot;
      return PhotoLibException(message, ex)
  }
  
  class PhotoLibException(message: String, ex: Exception?): RuntimeException(message, ex) {
  }
  </code></pre>
  <p>However, in this Kotlin example above, I am always invoking the super constructor with two args, and not invoking the constructor most appropriate to the situation. So what I have above works, but doesn't do exactly what it would do in Java where a different constructor is invoked in each situation.
  I also tried instantiating a new RuntimeException inside each fun above and casting it to PhotoLibException, but I wasn't allowed to do that.</p>
  <p>Can anyone suggest how I would do this correctly in Kotlin?</p>