Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scala/java中的异常_Java_Scala - Fatal编程技术网

scala/java中的异常

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

我是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)
  }
如果对格式错误的字符串(如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]
    作为函数返回。现在这可能更简单。