3

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.

1
  • have you checked in your executor logs whether that message is printed or not? Commented Sep 27, 2017 at 7:44

1 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.