问题标签 [custom-exceptions]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
235 浏览

c# - 自定义异常和减少重复代码

我决定在我的代码中使用异常来传递错误处理。每次我想创建一个新异常时,我都发现自己在重复代码。这些类没有什么特别的,只包含一个消息。但是在处理它们时,我已经开始依赖类型安全了。有没有办法提供一个新的异常类类型而不必重新实现构造函数?

对于我要定义的每种不同类型的异常,上面的代码重复了很多次。

0 投票
1 回答
113 浏览

java - Trying to suspend threads after custom exception called

This is a school assignment I have been working on. I've been back and forth with my tutor and he's been helping me with this but there's something I'm obviously missing here. The program in question takes an argument, "-f filename.txt" and in that filename are a list of things like this

The program does takes those names and creates classes using reflection and then calls the action() method of the generated classes (FansOn, etc.). It creates a thread for each class and then starts the thread, which generates a message like "Fan is on". On two of the events, PowerOut and WindowMalfunction, it throws a custom exception ControllerException which then needs to grab an object called GreenhouseControls and serialize it to a file called dump.out for later examination. The problem I'm having is being able to call my ghcSuspend() method which should suspend all events immediately after the exception call. In fact, at this point my program just generates massive stack overflow issues. I don't know if I should post my entire code here because I obviously don't want someone else taking my course to just get handed the bulk of code so I don't know what I should post to make you guys understand my issue.

So in my main() method, i create a GreenhouseControls object called gc. GreenhouseControls is the name of the main class file. Eventually, I need this gc object in my custom exception, which is defined and caught in my events.class file

0 投票
3 回答
185 浏览

c# - c#异常问题

谢谢大家的帮助。

当除数为 1 时,此代码不会产生我所期望的结果。未调用 exceptOne 的基类,未显示 exceptOne 中的超链接。我错过了什么?!

控制台输出为:

输入除数
1
WriteLine 异常 1...
WriteLine 异常 2...
基 ctor2
http : // exc2.com
最后写入行

0 投票
1 回答
42986 浏览

java - 使用 mockito 测试抛出未捕获的自定义异常的方法

如何编写基于 Mockito 的 JUnit 方法来测试此方法adduser()?我试着写一个,但它失败了,错误信息说异常没有被处理。错误显示为:

假设addUser()业务类中的方法永远不会捕获任何异常并且不会重新抛出。

测试用例方法:

在测试类中,我使用@Mock属性模拟服务层类并注入它。

请告诉我如何处理测试用例中的异常情况。

0 投票
3 回答
7408 浏览

jquery - 在 jquery 选择器路径中添加异常的最佳方法

假设我想绑定下面的所有项目

除了

我能想到

或者:

有什么更好的?

0 投票
2 回答
3125 浏览

java - 将 JSR303 JavaBean Validation 异常转换为自定义异常

我已经在我的 Web 应用程序的服务层中实现了 JSR303 JavaBean 验证(根据这篇文章)。现在我想将所有验证异常(例如 javax.validation.ConstraintViolationException)转换为我的自定义异常。

我创建了一个切面,只要在服务层中抛出异常就会调用它:

但是当我的服务使用 ConstraintViolationException 验证失败时,不会触发我的异常转换方面。我怀疑这是因为验证异常本身是由 Aspect 触发的:

如何以正确的顺序链接我的方面?先ValidateAspect,然后是ExceptionConversionAspect?

0 投票
1 回答
133 浏览

c# - 实验室网站的自定义异常处理和使用

我知道以前有人问过类似的问题,但这并不能消除我对此事的怀疑。

在我的所有页面 Page_Load 方法和调用连接到数据库的方法的所有其他方法周围,我将代码放在 try catch 语句中:

为每种类型的错误使用自定义异常是否明智?

  • 如果在我的数据库中没有找到一个组(如上面的代码);
  • 如果存在一个组;
  • 无法更新群组时;
  • 当一个组达到最大成员数 (5) 时;
  • ETC。;

还是最好限制这种错误处理方式?我知道为什么以及何时,但我不知道什么时候太多,或者你必须在哪里划清界限。

一个后续问题,我对一个组中的人数有限制,这个限制,我什么时候在我的代码中强制执行这个?在页面的类后面的代码中?或者当我连接到数据库时?或两者?

0 投票
2 回答
3192 浏览

java - 在 JSF 中,被调用的 EJB 抛出的自定义异常被视为 EJBTransactionRolledBackException 或 NullPointerException 或 ServletException

这就是问题所在:EJB 抛出了这个异常(来自 glassfish 日志):

在异常堆栈的客户端(在底部)我看到了这个:

当然,在浏览器中,我得到一个通用的 500 异常错误,表示存在空指针异常。

我想要一个自定义错误页面来检测“com.extremelatitudesoftware.els_commons.exceptions.LoginPreviousConfirmedException”并说“哎呀,你已经确认了!”

有人可以为我指出正确的方向,让客户端/JSF 层正确地“看到”异常,以便可以做到这一点。

这是问题的用例:
我在 EJB 层中有一个方法,用于检查用户之前是否确认了他们的登录帐户。如果他们没有,它会处理他们确认新帐户的请求。如果他们说回到旧电子邮件并说,嗯,让我们再次单击它,后端将检测到,抛出异常以使其被 JSF/客户端层拾取并通知用户该帐户已被确认。


添加示例代码:

方法检查条件并在必要时抛出异常。请注意,大多数情况下这不会发生,所以我想使用这种机制,而不是在每次有人确认他们的帐户时都拨打不必要的电话。

自定义异常:

这是在 JSF 层的 ManagedBean 中。它通过以下方式调用:preRenderView

这是日志显示的内容:

然后有几个追踪:



根据 BalusC 的建议,我将 ManagedBean 中的代码更改为:

这是在 web.xml

但我仍然得到这个(这是显示的错误页面的图像)。

0 投票
2 回答
489 浏览

php - PHP 自定义异常处理程序是否需要 try {} 捕获?

我编写了以下自定义异常处理程序:

并表示我想使用这个类及其方法来处理系统产生的所有异常和错误,方法如下:

我还表示我不希望在屏幕上显示错误并报告所有错误:

我现在的问题是 - 我还需要使用 try { } catch () { } 语句来捕获任何异常和错误吗?我知道以上内容很可能不是防弹的,但到目前为止似乎没有任何 try / catch 语句通过处理所有未捕获的异常和错误来工作。

另外 - 使用自定义异常处理程序并让它捕获所有未捕获的异常而不是通过 try {} catch (即性能/安全等)这样做有什么缺点吗?

0 投票
1 回答
741 浏览

java - Not able to catch custom exception of web service in their own catch blocks

We are using a web service which throws 3 types of custom web service faults i.e. ServiceException, ConnectionException and InvalidDataException.

Along with these catch blocks, we have also put in the catch block for RemoteAccessException which is the spring's runtime exception.

While testing the above listed custom exceptions, we found that all these 3 types of exceptions are not getting catched in their respective catch blocks. Instead all gets catched in the last catch block which is the RemoteAccessException. We found during debugging the exception object of soap fault which is of type org.springframework.remoting.jaxws.JaxWsSoapFaultException that the service is throwing the correct faults.

My concern is why these faults are not falling into their own catch blocks. The service itself tells us to handle these exceptions while placing the service call.

When I hit the service through proxy java client, the faults fall correctly into their respective blocks. If there would have been a problem with POJO's then they shouldn't have worked in this case also. But they are working in this case(when hit through java proxy client) which means no problem in POJOs.

We are using spring-2.5.6.jar.