13

What are the common standard exceptions in Scala? I am especially interested in how is .Net's NotImplementedException equivalent called?

UPDATE: The answer about the NotImplementedException seems to be org.apache.commons.lang.NotImplementedException

Ivan
  • 63,011
  • 101
  • 250
  • 382

3 Answers3

12

Almost nothing:

package scala {
  final class MatchError(obj: Any) extends RuntimeException
  final class UninitializedError extends RuntimeException("uninitialized value")
  final case class UninitializedFieldError (msg: String) extends RuntimeException(msg)

  package util.regex {
    class SyntaxError(e: String) extends RuntimeException(e)
  }

  package xml {

    class BrokenException() extends java.lang.Exception
    case class MalformedAttributeException(msg: String) extends RuntimeException(msg)

    package dtd {
      case class ValidationException(e: String) extends Exception(e)
    }

    package include {
      class CircularIncludeException(message: String) extends XIncludeException
      class UnavailableResourceException(message: String) extends XIncludeException(message)
      class XIncludeException(message: String) extends Exception(message)
    }

    package parsing {
      case class FatalError(msg: String) extends java.lang.RuntimeException(msg)
    }
  }
}

The rest comes from Java, which cover pretty much all corners. It begs the question of what these Scala methods throw on other platforms, doesn't it?

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
  • That gives a pretty fair idea of just how easy it is to throw your own custom Exceptions and even errors if it works with Java. Just make a one line class in an object and use as needed. final class MyException(arg:String) extends RuntimeException() or as seen in FatalError with java.lang.RuntimeException(msg). Nice one Sobral. – Andrew Scott Evans Oct 23 '15 at 18:10
2

The NotImplementedException is currently being considered for Scala 2.10, probably. See this thread.

axel22
  • 32,045
  • 9
  • 125
  • 137
  • 1
    `UnsupportedOperationException` of course isn't being considered for Scala 2.10. It has been part of the Java library for a long time. The thread is about adding a conveniece method that throws it to Predef. – Kim Stebel Oct 12 '11 at 18:35
  • 1
    First of all, UnsupportedOperationException and NotImplementedException mean totally different things and both are needed (I used to use both of them actively in C# projects of mine). And the question is what to use today, in Scala 2.9.1? I use to define a class and then implement it method-by-method, using "throw new NotImplementedException()" as not yet implemented methods bodies and compiling the project many times before I implement everything. But in Scala it seems like I have to implement all the functions by returning some dummy data - this may cause bugs and is just annoying. – Ivan Oct 12 '11 at 18:37
  • As of 2.10, `???` has been added to `Predef`. You can now write code like `class Thing { def getThing = ??? }`. Calling `getThing` will raise `scala.NotImplementedError` with the message "an implementation is missing". – afternoon Mar 04 '13 at 15:32
2

You can just use whatever default already exists in Java. Scala doesn't really add anything to the standard exceptions in Java.

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142