Reputation: 1672
How to catch exception , if my file is not found in given location in spark csv read?
try {
val df = sqlCxt.read.format("csv")
.option("delimiter", "|")
.load("C:/Users/swapnil.shirke/Downloads/dataset/u.item");
}
catch {
case ex: FileNotFoundException => {
println(" file missing exception")
}
case ex: Exception => ex.printStackTrace()
}
given try catch condition not worked.
Upvotes: 3
Views: 1675
Reputation: 41987
In scala, if the file path is not correct then the exception is org.apache.spark.sql.AnalysisException: Path does not exist: ....
so the correct way is
try {
val df = sqlCxt.read.format("csv")
.option("delimiter", "|")
.load("/Users/swapnil.shirke/Downloads/dataset/u.item");
}
catch {
case ex: AnalysisException => {
println(" file missing exception")
}
case ex: Exception => ex.printStackTrace()
}
I hope the answer is helpful
Upvotes: 4