Hat jemand gute Beispiele für die Verwendung scala.util.control.Exception
version 2.12.0 (version 2.8.0),? Ich kämpfe, um es von den Typen herauszufinden.Verwendung von scala.util.control.Exception
Antwort
In der Tat - ich finde es auch ziemlich verwirrend! Hier ist ein Problem, wo ich eine Eigenschaft haben, die ein parseable veraltet sein können:
def parse(s: String) : Date = new SimpleDateFormat("yyyy-MM-dd").parse(s)
def parseDate = parse(System.getProperty("foo.bar"))
type PE = ParseException
import scala.util.control.Exception._
val d1 = try {
parseDate
} catch {
case e: PE => new Date
}
Schalt dies auf eine funktionelle Form:
val date =
catching(classOf[PE]) either parseDate fold (_ => new Date, identity(_))
In dem obigen Code, schaltet sich catching(t) either expr
in einem Either[T, E]
führen wird, wo T
ist der Typ des Throwable und E
ist der Typ des Ausdrucks. Dieser kann dann über eine fold
in einen bestimmten Wert umgewandelt werden.
Oder eine andere Version wieder:
val date =
handling(classOf[PE]) by (_ => new Date) apply parseDate
Dies ist vielleicht ein wenig klarer - das sind äquivalent:
handling(t) by g apply f
try { f } catch { case _ : t => g }
Hier einige Beispiele:
Compiler/scala/tools/nsc/interpreter/InteractiveReader.scala
def readLine(prompt: String): String = {
def handler: Catcher[String] = {
case e: IOException if restartSystemCall(e) => readLine(prompt)
}
catching(handler) { readOneLine(prompt) }
}
Compiler/scala/tools/nsc/Interpreter.scala
/** We turn off the binding to accomodate ticket #2817 */
def onErr: Catcher[(String, Boolean)] = {
case t: Throwable if bindLastException =>
withoutBindingLastException {
quietBind("lastException", "java.lang.Throwable", t)
(stringFromWriter(t.printStackTrace(_)), false)
}
}
catching(onErr) {
unwrapping(wrapperExceptions: _*) {
(resultValMethod.invoke(loadedResultObject).toString, true)
}
}
...
/** Temporarily be quiet */
def beQuietDuring[T](operation: => T): T = {
val wasPrinting = printResults
ultimately(printResults = wasPrinting) {
printResults = false
operation
}
}
/** whether to bind the lastException variable */
private var bindLastException = true
/** Temporarily stop binding lastException */
def withoutBindingLastException[T](operation: => T): T = {
val wasBinding = bindLastException
ultimately(bindLastException = wasBinding) {
bindLastException = false
operation
}
}
Compiler/scala/tools/nsc/io/Process.scala
def exitValue(): Option[Int] =
catching(classOf[IllegalThreadStateException]) opt process.exitValue()
Bibliothek/scala/xml/include/sax /Main.scala
def saxe[T](body: => T) = catching[T](classOf[SAXException]) opt body
...
ignoring(classOf[SAXException]) {
includer.setProperty(lexicalHandler, s)
s setFilter includer
}
Kannst du ein bisschen auf den Fang ... Auspacken Beispiel? –
- 1. Verwendung von "Verwendung" Anweisung zu entsorgen
- 2. Verwendung des Spaltennamens bei Verwendung von SqlDataReader.IsDBNull
- 3. Verwendung von SpringTemplateEngine bei Verwendung von Spring Boot
- 4. Verwendung von Crashlytics ohne Verwendung von Fabric in iOS
- 5. Die Verwendung von UIApplicationDidFinishLaunchingNotification
- 6. Verwendung von __init__.py
- 7. File.ReadAllText Verwendung von CACHE
- 8. Verwendung von std :: shared_ptr
- 9. Verwendung von Auszug
- 10. Praktische Verwendung von Ausdrucksbäumen
- 11. Sonderbare Verwendung von Generika
- 12. Verwendung von CoTaskMemAlloc?
- 13. falsche Verwendung von sprintf?
- 14. Verwendung von Twitter OpenAuth
- 15. Verwendung von generischen Delegaten
- 16. Verwendung von ToTitleCase
- 17. Dynamische Verwendung von typeof
- 18. Verwendung von statischen Schlüsselwort
- 19. Verwendung von undefined Konstante
- 20. Ungültige Verwendung von Nullwert
- 21. Überprüfung von jQuery Verwendung
- 22. Verwendung von sqlite3_exec
- 23. mit der Verwendung von +
- 24. Verwendung von Halogenid Profiler
- 25. Verwendung von Logical Masken
- 26. Korrekte Verwendung von Objekten
- 27. Verwendung von LocalMessageSender synchron
- 28. Verwendung von xcasesets
- 29. Bevorzugte Verwendung von Favicons?
- 30. Die Verwendung von executor.shutdown()
Eine weitere Alternative: 'failAsValue (classOf [PE]) (neues Datum) {parseDate}'. :) – missingfaktor