我验证了我的函数,如果在验证过程中抛出一个异常,我希望在catch和return中停止该方法,由于某种原因,它会继续并且只能在主尝试中捕获/抓住。为什么scala在Catch中没有完成功能
代码:
def updateProduct(request: UpdateProductRequest): BaseResponse[String] =
{
try
{
try
{
ValidateUpdateProductRequest(request)
}
catch
{
case ex: Exception => {
val errorResponse:ErrorResponse[String] = ErrorResponse(ErrorCode.InvalidParameters, ex.getMessage, 500)
errorResponse // <=- This does not return from function.. In debug i get here
}
}
val deleteProductResult = productRepository.updateProduct(request) //I dont want to get here !!
DTOResponse(deleteProductResult)
}
catch
{
case ex: Exception => {
Logger.error("Failed to update product Id = " +request.product.id, ex);
var errorResponse:ErrorResponse[String] = ErrorResponse(ErrorCode.GeneralError, ex.getMessage, 500)
errorResponse
}
}
}
我明白阶的函数的最后一行是唯一的地方函数将返回,让我怎么从抓回来吗?
原因是我想使用在BaseResponse [字符串]
由于不同错误代码!
愚蠢的问题,但你肯定会抛出一个异常在你的'ValidateUpdateProductRequest'方法吗?这可能是因为你传入的主要'catch'是因为另一个异常,例如在'updateProduct'中发生了... –
是的,我一步一步地调试它,我越来越接近第二次尝试..但函数不返回,它继续val val deleteProductResult = productRepository.updateProduct(..),然后得到空引用,因为产品为空 - 这就是验证的一部分.. – ilansch