在Scala对象构造函数中处理异常 [英] Handling exceptions in Scala object constructors

查看:67
本文介绍了在Scala对象构造函数中处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Lift JPA,当我引用Model时,它调用了超级构造函数,但我遇到了异常:

I'm trying to use Lift JPA and when I reference Model, it calls the super constructor I'm getting an exception:

object Model extends LocalEMF("LiftPersistenceUnit") with RequestVarEM

问题在于该异常隐藏在该异常的后面:

The problem is that the exception is hidden behind this exception:

java.lang.NoClassDefFoundError: Could not initialize class...

所以,我的问题是:在这种情况下记录/处理异常的最佳方法是什么?

So, my question is: what is the best way to log/handle exceptions in this case?

或者,有人可以推荐另一种模式来使用Lift JPA吗?推荐使用模型作为代码的方式,但这不是非常用户友好的IMO.这个想法是要有一个单例实体管理器工厂,可以通过一个请求变量来访问它.sc

Alternatively, can anyone recommend another pattern to use Lift JPA? The way Model has been code is the recommended way, but it's not very user-friendly IMO. The idea is to have a singleton entity manager factory which is accessible via a request variable.sc

推荐答案

我可以想到在LocalEMF的构造函数之前执行某些操作的唯一方法:

I can think of the only way to execute something prior to LocalEMF's constructor:

class Model(val init: Unit = println("Hai")) 
  extends LocalEMF("LiftPersistenceUnit") with RequestVarEM

也许您可以代理调用LocalEMF("LiftPersistenceUnit")的电话,并使用一些特殊技巧来添加适当的异常登录记录.

Maybe you could proxy a call to LocalEMF("LiftPersistenceUnit"), and add appropriate logging on exceptions, using some extraordinary trick.

编辑

我找到了:

class LocalEMF(haha: String) { val e = throw new RuntimeException }
trait RequestVarEM

class Model(val init: Unit = println("Hai")) extends { val e = 
  try new LocalEMF("LiftPersistenceUnit") catch {
    case t: Throwable => println("Catched: " + t); throw t
  } 
} with RequestVarEM

这篇关于在Scala对象构造函数中处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆