scala/java中的异常
我是scala编程和jvm语言的初学者。我想将scala/java中的异常,java,scala,Java,Scala,我是scala编程和jvm语言的初学者。我想将yyyy-MM-dd格式的字符串转换为如下日期格式: import java.text.SimpleDateFormat import java.util.Date val format = new SimpleDateFormat("yyyy-MM-dd") def strTodate(stringDate: String): Date = { format.parse(stringDate) } 如果对格式错误的字符串(如str
yyyy-MM-dd
格式的字符串转换为如下日期格式:
import java.text.SimpleDateFormat
import java.util.Date
val format = new SimpleDateFormat("yyyy-MM-dd")
def strTodate(stringDate: String): Date = {
format.parse(stringDate)
}
如果对格式错误的字符串(如strotdate(“18/03/03”)调用了strotdate
),如何处理异常?我要处理异常并打印字符串处理异常:
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
object app {
val format = new SimpleDateFormat("yyyy-MM-dd")
def strTodate(stringDate: String): Either[Exception, Date] = {
try {
Right(format.parse(stringDate))
} catch {
case ioException : IOException =>
Left(ioException)
case e: Exception =>
Left(e)
}
}
def main(args: Array[String]) : Unit =
strTodate("2018-02-02") match {
case Right(date) => println(date)
case Left(err) => println(err.getMessage)
}
}
处理例外情况:
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.Date
object app {
val format = new SimpleDateFormat("yyyy-MM-dd")
def strTodate(stringDate: String): Either[Exception, Date] = {
try {
Right(format.parse(stringDate))
} catch {
case ioException : IOException =>
Left(ioException)
case e: Exception =>
Left(e)
}
}
def main(args: Array[String]) : Unit =
strTodate("2018-02-02") match {
case Right(date) => println(date)
case Left(err) => println(err.getMessage)
}
}
scala有三种处理错误的方法
选项
:有无
或部分
尝试
:成功或失败或
:有左或右<代码>正确始终是正确的结果other
,下面是如何做other[String,Date]
的方法,左边是String
,右边是Date
例如
import java.text.SimpleDateFormat
import java.util.Date
import scala.util.Try
import scala.util.{Failure, Success}
val format = new SimpleDateFormat("yyyy-MM-dd")
def strTodate(stringDate: String): Either[String, Date] = {
Try {
format.parse(stringDate)
} match {
case Success(s) => Right(s)
case Failure(e: ParseException) => Left(s"bad format: $stringDate")
case Failure(e: Throwable) => Left(s"Unknown error formatting : $stringDate")
}
}
val date1 = strTodate("2018-09-26")
println(date1) // Right(Wed Sep 26 00:00:00 PDT 2018)
val date2 = strTodate("2018/09/26")
println(date2) // Left(bad format: 2018/09/26)
scala有三种处理错误的方法
选项
:有无
或部分
尝试
:成功或失败或
:有左或右<代码>正确始终是正确的结果other
,下面是如何做other[String,Date]
的方法,左边是String
,右边是Date
例如
import java.text.SimpleDateFormat
import java.util.Date
import scala.util.Try
import scala.util.{Failure, Success}
val format = new SimpleDateFormat("yyyy-MM-dd")
def strTodate(stringDate: String): Either[String, Date] = {
Try {
format.parse(stringDate)
} match {
case Success(s) => Right(s)
case Failure(e: ParseException) => Left(s"bad format: $stringDate")
case Failure(e: Throwable) => Left(s"Unknown error formatting : $stringDate")
}
}
val date1 = strTodate("2018-09-26")
println(date1) // Right(Wed Sep 26 00:00:00 PDT 2018)
val date2 = strTodate("2018/09/26")
println(date2) // Left(bad format: 2018/09/26)
您的
stringDate
示例是什么??这也是java的问题,因为时间api来自java。要处理错误,您可以使用scala.util.Try
我已经用一个我想作为例外处理的格式错误字符串的示例更新了这个问题,请参见和您的stringDate
示例是什么??这也是java的问题,因为时间api来自java。要处理错误,您可以使用scala.util.Try
我已经用一个格式不正确的字符串示例更新了问题,我想将其作为例外处理请参见,谢谢。我有一个关于的问题,是不是键入日期?因为这样做的目的是在case Success、print Failure和discard correct的情况下使用日期,所以您可以将[String,Date]
读为“我的函数将返回字符串(失败时)或日期(成功时)”Right(value)
wrapps success value。在函数式编程中,函数必须始终返回某些内容,这意味着您可以返回实际成功的Date
或以某种方式返回失败的消息。您还需要了解模式匹配(您在上述情况下看到匹配,在命令式编程中将其视为其他情况). 好的,只需将Right
替换为Some
,然后将None
替换为Left()
,您将得到选项[Date]
作为函数返回。现在可能更简单。谢谢。我有一个关于的问题,是不是键入日期?因为这样做的目的是在case Success、print Failure和discard correct的情况下使用日期,所以您可以将[String,Date]
读为“我的函数将返回字符串(失败时)或日期(成功时)”Right(value)
wrapps success value。在函数式编程中,函数必须始终返回某些内容,这意味着您可以返回实际成功的Date
或以某种方式返回失败的消息。您还需要了解模式匹配(您在上述情况下看到匹配,在命令式编程中将其视为其他情况). 好的,只需将Right
替换为Some
,然后将None
替换为Left()
,您将得到选项[Date]
作为函数返回。现在这可能更简单。