简体   繁体   English

从库调用时如何引发异常?

[英]How to throw exceptions when called from library?

I am defining a convention for the AutoMapping in Fluent NHibernate. 我正在为Fluent NHibernate中的自动映射定义一个约定。 From within this convention, I throw an exception: 从这个约定中,我抛出一个异常:

public void Apply(IManyToManyCollectionInstance instance){
  ...
  if(<error>)
    throw new MappingException("Invalid Mapping");

  <doSomeStuff>
}

In another class, I initialize the configuration, resulting in the code above being executed: 在另一个类中,我初始化配置,导致上面的代码被执行:

public void init(){
  try{
    SessionFactory = configuration.BuildSessionFactory(); // Apply(...) will be called here
  }
  catch (MappingException ex)
  {
    <catchError>
  }

When <error> is true, <doSomeStuff> is highlighted by VisualStudio and I am warned that "MappingException was unhandled by user code". <error>为true时, <doSomeStuff>将突出显示<doSomeStuff> ,并且警告我“ MappingException未由用户代码处理”。

Why is the exception not thrown until <catchError> ? 为什么直到<catchError>才引发异常?

The Fluent NHibernate library is added by the NuGet package manager. Nuent软件包管理器添加了Fluent NHibernate库。 If this is the reason that the exception is not forwarded, how can I send the MappingException to <catchError> ? 如果这是未转发异常的原因,如何将MappingException发送到<catchError>

The reason for this behavior most likely is that you have checked the following option: Debug -> Exceptions -> Column "Thrown" for "Common Language Runtime Exceptions". 出现这种现象的原因很可能是您检查了以下选项:调试->异常->“公共语言运行时异常”的“抛出”列。

If you hit F5 or otherwise continue debugging, the code will eventually reach <catchError> . 如果您按F5或以其他方式继续调试,则代码最终将到达<catchError>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM