我在spark-scala代码中定义了一个变量,如下所示,并尝试catch块。
var exceptionFlag = ""
try{
exceptionFlag = "FALSE"
}
catch{
exceptionFlag = "TRUE"
}
在这个try catch块之后,我需要编写基于标志的逻辑。
if (exceptionFlag = "TRUE"){
write the error details to hive...
}
我定义的变量不能在catch块中访问,所以我不能设置标志。
你能建议一下如何处理这种情况吗?
谢谢,鲍勃
发布于 2018-06-06 06:11:55
试试..。catch是Scala中的一个表达式,您可以在不使用var的情况下执行相同的操作:
scala> :paste
// Entering paste mode (ctrl-D to finish)
def test(): Unit ={
val exceptionFlag = try{
val x=10/0
"FALSE"
}
catch{
case ex: Exception=> "TRUE"
}
if (exceptionFlag == "TRUE"){
println("write the error details to hive...")
}
}
// Exiting paste mode, now interpreting.
test: ()Unit
scala> test()
write the error details to hive...
scala>
发布于 2018-06-06 03:23:45
你可以这样做
object Driver{
def test(): Unit ={
var exceptionFlag = ""
try{
val x=10/0
exceptionFlag = "FALSE"
}
catch{
case ex: Exception=> exceptionFlag = "TRUE"
}
if (exceptionFlag == "TRUE"){
println("write the error details to hive...")
}
}
def main(arr:Array[String]) {
test
}
}
示例输出:
write the error details to hive...
https://stackoverflow.com/questions/50707020
复制相似问题