2017-08-24 52 views
0

內部處理:斯卡拉錯誤我有我下面的Scala代碼故障功能

class ClassMyHelper { 

    protected var logger: Logger = LogManager.getLogger(classOf[AvroHelper]) 

    def somefunc(schema: Schema, datum: GenericRecord): Array[Byte] = { 
    <code> 
    var byteData: Array[Byte] = null 
    try { 
     <code> 
     byteData = os.toByteArray() 
     //byteData 
    } catch { 
     case e: IOException => 
     logger.error("IOException encountered!! ", e) 

     case e: Exception => 
     logger.error("Something went wrong!! ", e) 

    } finally try os.close() 
    catch { 
     case e: IOException => 
     logger.error("IOException encountered while closing output stream!! ", e) 

     case e: Exception => 
     logger.error("Something went wrong while closing output stream!! ", e) 

    } 
    byteData //Unreachable code 
    } 
} 

的問題是,在somefunc函數的最後一行我得到一個無法訪問的代碼錯誤。 你能幫我確定我在這裏做錯了什麼嗎?

回答

1

如果第二catch塊的東西后添加finally {}出現清理。我不知道爲什麼。我從來沒有使用過try/catch/finally。我更喜歡標準庫中的Scala Try類。

順便說一句,下次您發佈的代碼,請包括所需import s和檢查,以確保您的代碼編譯爲呈現。