[英]scala Error handling inside a function
我的以下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函數的最后一行出現了無法到達的代碼錯誤。 您能幫我確定我在做什么錯嗎?
如果您在第二個catch
塊之后添加了一個finally {}
,則可能會清除所有內容。 我不知道為什么。 我自己從來沒有try/catch/finally
。 我更喜歡標准庫中的Scala Try
類。
順便說一句,下一次您發布代碼時,請包括所需的import
,並檢查以確保您的代碼可以按所示進行編譯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.