What are Scala standard exceptions?
Asked Answered
U

3

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

Uplift answered 12/10, 2011 at 18:24 Comment(0)
R
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?

Radiancy answered 12/10, 2011 at 19:0 Comment(1)
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.Flexion
W
2

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

Wilfordwilfred answered 12/10, 2011 at 18:29 Comment(3)
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.Hickox
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.Uplift
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".Folliculin
H
2

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

Hickox answered 12/10, 2011 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.