我不知道这有什么问题,但instanceof似乎不起作用。
应用错误.ts
class AppError extends Error {
public statusCode;
constructor(message, statusCode) {
super(message);
this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
this.statusCode = statusCode || 500;
}
}
export default AppError;
Run Code Online (Sandbox Code Playgroud)
错误请求错误.ts
import AppError from "./AppError";
class BadRequestError extends AppError {
constructor(message?: string) {
super(message || "Client sent a bad request", 400);
}
}
export default BadRequestError;
Run Code Online (Sandbox Code Playgroud)
处理程序
try {
throw new BadRequestError();
} catch (err) {
if (err instanceof AppError) {
responseCallback(err.statusCode, err.message, callback);
} else {
responseCallback(500, "Internal Server …Run Code Online (Sandbox Code Playgroud) 我有一个自定义异常类:
public class MyException: Exception
{
public MyException(MyExceptionEnum myError) : base(myError.ToDescription()) { }
public MyException(MyExceptionEnum myError, Exception innerException) : base(myError.ToDescription(), innerException) { }
}
Run Code Online (Sandbox Code Playgroud)
.ToDescription是一种扩展方法,MyExceptionEnum用于为异常错误详细信息提供枚举到字符串映射.
这是我扔它的方式:
if (someCondition)
throw new MyException(MyExceptionEnum.SomeError);
Run Code Online (Sandbox Code Playgroud)
所以我使用我的第一个ctor,它创建一个带有给定消息的新Exception.
现在进入控制器:
[HttpPost]
public ActionResult UpdateFoo(Foo model)
{
try
{
_fooService.UpdateModel(model);
_unitOfWork.Commit();
}
catch(MyException myException)
{
ViewData.ModelState.AddModelError("ModelErrors", myException);
}
return View("Index", model);
}
Run Code Online (Sandbox Code Playgroud)
最后来自View的片段:
<%: Html.ValidationMessage("ModelErrors") %>
Run Code Online (Sandbox Code Playgroud)
不起作用(我调试时抛出异常,错误被添加到模型状态,但页面上没有显示).
但是,如果我改为以下行:
ViewData.ModelState.AddModelError("ModelErrors", myException.Message);
Run Code Online (Sandbox Code Playgroud)
有用.
AddModelError 有两个重载:
那么第一次过载有什么用呢?我的异常确实有一个内部异常消息,所以我认为HTML扩展会呈现这个?
我们如何使用ModelState处理自定义异常呢?使用第二次过载是否正确?
c# custom-exceptions modelstate asp.net-mvc-2-validation asp.net-mvc-2
问题如下:EJB 抛出此异常(来自 glassfish 日志):
SEVERE: Attempting to confirm previously confirmed login using confirmation UUID: b90b33ca-dc69-41c1-9c60-99152810c89b
com.extremelatitudesoftware.els_commons.exceptions.LoginPreviouslyConfirmedException: Attempting to confirm previously confirmed login using confirmation UUID: b90b33ca-dc69-41c1-9c60-99152810c89b
at com.extremelatitudesoftware.security.auth.CredentialsController.checkForPreviousConfirmation(CredentialsController.java:244)
Run Code Online (Sandbox Code Playgroud)
在异常堆栈的客户端(在底部)我看到:
WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.NullPointerException
at com.extremelatitudesoftware.accesscontrol.registration.RegistrationConfirmationBean.setChallengeQuestion(RegistrationConfirmationBean.java:61)
at com.extremelatitudesoftware.accesscontrol.registration.RegistrationConfirmationBean.fetchChallengeResponse(RegistrationConfirmationBean.java:51)
Run Code Online (Sandbox Code Playgroud)
当然,在浏览器中我收到一个通用 500 异常错误,表示存在空指针异常。
我想要一个自定义错误页面来检测“com.extremelatitudesoftware.els_commons.exceptions.LoginPreviouslyConfirmedException”并说“哎呀,你已经确认了这一点!”
有人可以为我指出正确的方向,让客户端/JSF 层正确地“看到”异常,以便可以完成此操作。
这是该问题的用例:
我在 EJB 层中有一个方法,用于检查用户之前是否已确认其登录帐户。如果他们还没有,它会处理他们确认新帐户的请求。如果他们说回到旧电子邮件并说,嗯,让我们再次单击该电子邮件,后端将检测到这一点,并抛出异常,以便 JSF/客户端层拾取该异常,并且将通知用户该帐户已被确认。
添加示例代码:
方法检查条件并在必要时抛出异常。请注意,大多数情况下不会发生这种情况,因此我想使用此机制,而不是每次有人确认其帐户时都进行不必要的调用。
...
private void checkForPreviousConfirmation(Credentials cr)
throws LoginPreviouslyConfirmedException {
if (!(CredentialsStatusType.PENDING.equals(cr.getStatus()))
|| !(CredentialsDispositionType.WAITING.equals(cr.getDisposition()))) {
String msg = "Attempting to confirm previously …Run Code Online (Sandbox Code Playgroud) 如何拥有MyException和MyRuntimeException使用相同的自定义getMessage()实现?
由于Java没有多重继承,我不知道该怎么做.目前我在这两个类中都有重复的代码......
重要细节:getMessage()像this.class.getName()这样的东西.所以我确实需要getMessage()使用反射,因为我需要对象的类名进行本地化.
所以要么我需要一个解决方案来解决我的第一个问题,要么解决如何在静态方法中使用反射的原因然后我可以使用一些两个例外都可以使用的实用程序类?
一个解决方案可能是某个帮助器类中的静态方法,然后使用它:
return new Object(){}.getClass().getEnclosingClass().getEnclosingClass();
不是吗?
我想扩展在调用getMessage时返回自定义消息的异常类.
class MY_Exceptions extends CI_Exceptions{
function __construct(){
parent::__construct();
}
function getMessage(){
$msg = parent::getMessage();
return "ERROR - ".$msg;
}
}
Run Code Online (Sandbox Code Playgroud)
MY_Exceptions放在核心文件夹中.抛出/处理异常如下:
try{
throw new Exception('a message');
}catch (Exception $e) {
echo $e->getMessage();
}
Run Code Online (Sandbox Code Playgroud)
目的是得到"错误 - 一条消息".但它总是返回"消息".当我尝试调试时,控件永远不会进入MY_Exception类.有什么我想念的吗?
如果我有以下
function ValidationException(nr, msg){
this.message = msg;
this.name = "my exception";
this.number = nr;
}
function myFunction(dayOfWeek){
if(dayOfWeek > 7){
throw new ValidationException(dayOfWeek, "7days only!");
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:如何在catch块中捕获此特定异常?
javascript exception-handling try-catch throw custom-exceptions
我正在创建一个带有3个输入字段(小时,分钟和秒)的小Timer程序。由于某些原因,OverTwentyFourException异常不适用于小时数输入字段。这是我的代码的一部分:
static JTextField userInputHrs;
static JTextField userInputMins;
static JTextField userInputSecs;
static int hrsChosen;
static int minsChosen;
static int secsChosen;
startButton.addActionListener(e -> {
switch(startButton.getIcon().toString()) {
case "Pause":
timer.TimerFunctionality.pause();
break;
case "Resume":
timer.TimerFunctionality.resume();
break;
default:
try {
//Calculate seconds from user input
hrsChosen = Integer.parseInt(userInputHrs.getText());
minsChosen = Integer.parseInt(userInputMins.getText());
secsChosen = Integer.parseInt(userInputSecs.getText());
secsRemaining = hrsChosen * 3600 + minsChosen * 60 + secsChosen;
if(hrsChosen < 0 || minsChosen < 0 || secsChosen < 0)
throw new NegativeException();
if(hrsChosen > 24)
throw …Run Code Online (Sandbox Code Playgroud) 我有一个资源:
@GET
@Path("/print-order")
@Produces("application/pdf")
public byte[] printOrder(@QueryParam("id") Long orderId) {
return ...;
}
Run Code Online (Sandbox Code Playgroud)
...这可能会引发与用户相关的错误,并且必须显示为HTML页面。所以我实现了一个,ExceptionMapper但我不知道如何获取@Produces("application/pdf")被调用资源的注释的值。
@Provider
public class CustomExceptionMapper implements ExceptionMapper<CustomException> {
@Override
public Response toResponse(CustomException exception) {
if (contentType = "application/pdf")
... html respone
else
... entity response
}
}
Run Code Online (Sandbox Code Playgroud)
我在Jersy 1.12实现中使用JAX-RS 1.x(jsr311),但希望拥有独立于实现的解决方案。
我正在为大学准备一个项目,我需要编写一个自定义异常,当它们未正确初始化时,将由同一个包中的几个类抛出.问题是我必须让用户知道哪些类没有正确初始化(并抛出异常)......所以我在考虑这样的事情:
class InitializationException extends Exception {
private static final String DEFAULT_MSG =
"This " + CLASSNAME-THROWINGME + " had not been initialized properly!";
protected String msg;
InitializationException() {
this.msg = DEFAULT_MSG;
}
InitializationException(String msg) {
this.msg = msg;
}
}
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,可以通过反思来实现吗?)
catch (Exception ex)
{
_errorCode = ErrorCodes.SqlGeneralError;
CommonTools.vAddToLog(ex, _errorCode, _userID);
throw new JOVALException(_errorCode);
}
Run Code Online (Sandbox Code Playgroud)
我使用这个代码的和平来处理调用的自定义异常的错误(JOVALException)但是当发生异常时它打开"No source available"页面并且告诉我不是堆栈跟踪 是空的我怎么能解决这个问题?
编辑
public JOVALException(ErrorCodes _errCode, String _message = "")
{
ErrMessage = _message;
ErrCode = _errCode;
}
Run Code Online (Sandbox Code Playgroud)
这是我的构造函数,如何修改它?
java ×5
exception ×4
c# ×2
codeigniter ×1
ejb ×1
javascript ×1
jax-rs ×1
jsf-2 ×1
modelstate ×1
node.js ×1
servlets ×1
throw ×1
try-catch ×1