Tag: exception处理

在java中使用assert有什么用

可能重复: 断言做了什么? 断言测试程序员在开发过程中的假设,而不为exception编写exception处理程序这是我得到的,当我在搜索断言时 。 除此之外,人们还说,它是exception处理的替代方案。 当您不想花时间编写exception处理代码时,断言就会出现。 但是,我没有得到工作和使用。 有人解释这个例子。 class AssertExample { public static void main(String[] args) { int x = 0; assert (x > 0) ? “assertion failed” : “assertion passed”; System.out.println(“finished”); } }

Java EE App的理想错误页面

我很难在我的应用程序中整合错误。 目前,我的error.jsp看起来如下(部分): 除了!之外的所有场景都可以正常工作:有时在我的应用程序中,我使用以下代码捕获MyException类中的内置exception: catch(MyException ex){ log.error(ex.getMessage(), uivex); String originalURL = “/errorpages/error.jsp?errorcode=” + (ex.getMajor() + ex.getMinor()) + “&errormessage=” + ex.getMessage(); RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address); dispatcher.forward(request,response); } 现在的问题是,当我转发到error.jsp页面时…而不是看到来自MyException类的实际错误..我看到NullPointerException因为javax.servlet.error.status_code没有任何内容,并且页面被声明as isErrorPage=”true” 在这种情况下我该怎么办? 一种解决方案是创建一个完全不同的error.jsp(将其命名为error1.jsp)页面,并将MyException类中的exception转发到该页面。 虽然,我想把所有东西放在一个地方。

@ControllerAdviceexception处理程序方法未被调用

我正在为Spring MVC中的exception处理示例演示应用程序。我正在尝试Exception Handling With @ControllerAdvice 我按照此链接中描述的步骤进行操作。 但是当我运行我的应用程序时,我得到以下错误 org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.test.core.ApplicationException 有关详细信息,请参阅我正在处理的课程 ApplicationException.java public class ApplicationException extends RuntimeException{ /** * */ private static final long serialVersionUID = -9061684361606685158L; private ErrorStatus errorStatus; private int msgNumber; private Object []args; public ApplicationException(){} public ApplicationException(ErrorStatus errorStatus, int msgNumber, Object[] args) { this.errorStatus = errorStatus; this.msgNumber […]

仅当没有Exception的映射时,才会调用@ExceptionHandler for Error

使用spring-web-4.2.6,我有以下Controller和ExceptionHandler: @ControllerAdvice public class ExceptionsHandler { @ExceptionHandler(Exception.class) public ResponseEntity HandleDefaultException(Exception ex) { … } @ExceptionHandler(InternalError.class) public ResponseEntity HandleInternalError(InternalError ex) { … } } @RestController @RequestMapping(“/myController”) public class MyController { @RequestMapping(value = “/myAction”, method = RequestMethod.POST) public boolean myAction() { throw new InternalError(“”); } } 出于某种原因,调用ExceptionsHandler的HandleDefaultException(对于Exception.class)方法,但类型为NestedServletException,而不是HandleInternalError调用。 删除默认调用时,将使用正确的InternalErrorexception调用IntenalError调用。 我不想删除默认调用,因为对我来说,拥有一个默认处理程序以便为我的用户提供更好的体验非常重要。 我在这里想念的是什么? 编辑: 显然我正在使用spring-web-4.3.3,而不是要求它。 我不明白为什么,这是我的Gradle依赖树: http : //pastebin.com/h6KXSyp2

如何使用JNI传递和接收对象

我有一个JAVA应用程序,我希望使用JNI将对象作为参数传递给C代码,并且我想再次使用JNI从C代码接收对象到JAVA 。 在JAVA方面,我只是创建了一个应用程序并将其传递给方法,如下所示 JlibFprint.fp_image_data fpimg = new JlibFprint.fp_image_data(); //object to be pass //fp_image_data is the static inner class of the class JlibFprint JlibFprint.fp_image_data fpimg1 = new JlibFprint.fp_image_data(); //received object 这个对象传递给像这样的方法 fpimg1 = JlibFprint.binary_image(fpimg); 该方法的JNI代码如下所示: JNIEXPORT jobject JNICALL Java_jlibfprint_JlibFprint_binary_1image(JNIEnv *env, jclass jcls,jobject imgobj) { struct fp_img img; struct fp_img *imgptr; imgptr = &img; jfp2cfp(env,imgobj,imgptr); fp_init(); imgptr […]

Javaexception处理 – 捕获超类exception

我有一个关于在Web应用程序中处理exception的问题。 我经常听到抓住超类exception是一个坏主意。 我经常编写代码来捕获struts action / java servlet类中的所有exception。 try { // call business facade // business facade calls DAO // any exception from DAO bubbles up } catch (Exception e) { log.error(“error”, e); } 如果我们不捕获超类Exception。 我们如何处理任何意外的运行时错误并适当地记录它们

酷还是傻? Catch(exception e)

我正在写一些代码,我注意到exception处理中的一个模式让我思考: try{ // do stuff… throws JMS, Create and NamingException } catch (NamingException e) { log1(e); rollback(); doSomething(e) } catch (CreateException e) { log1(e); rollback(); doSomething(e) } 其中JMSException将处理堆栈中的某些位置。 是不是只写: try{ // do stuff… throws JMS, Create and NamingException } catch Exception[NamingException, CreateException] e) { log1(e); rollback(); doSomething(e) } 而不是把它放在一个辅助方法: try{ // do stuff… throws JMS, […]

Spring Transaction中是否需要exception处理?

我对使用Transaction进行exception处理有疑问。 为了清楚说明我的问题,我想展示我的配置: 活动事务类是: @Transactional public class CustomerService extends BaseService implements ICustomerService { @Transactional(readOnly = true) public Customer getCustomerById(String id) { return getDaoProvider().getCustomerDao().getCustomerById(id); } @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = { Throwable.class }) public void addNewCustomer(CustomerDTO customerDTO) { Customer customer = new Customer(); customer.setCustomerId(customerDTO.getCustomerId()); customer.setCustomerName(customerDTO.getCustomerName()); customer.setActive(customerDTO.isActive()); getDaoProvider().getCustomerDao().save(customer); } } 我的疑惑在于addNewCustomer方法。 我已经设置了rollbackFor = { Throwable.class […]

javaexception会终止整个java应用程序吗?

我曾经认为,当发生exception时,整个java应用程序将被终止。 例如,我编写了一个测试函数来测试我的想法。 public void test(){ File fileDir=new File(sourceDataDir); if(fileDir.exists()){ File[] files = fileDir.listFiles(); for(int index=0 ; index<files.length ; index++){ System.out.println("index = "+index); File file = files[index]; if(index == 1)//delete a file to cause a FileNotFoundException file.delete(); try { BufferedReader in = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } } } } 我删除文件以手动导致FileNotFoundException […]

Java Mail超时和connectiontimeout处理

我正在使用JavaMail向SMTP服务器发送电子邮件请求。 我想在我的代码中设置“mail.smtp.connectiontimeout”和“mail.smtp.timeout”属性。 从编程方面来说,我希望在Java中达到超时和/或connectiontimeout操作时捕获它们并相应地处理事情。 在这个意义上的处理,我需要在下次再次重试同一封电子邮件。 如何在Java / JavaMail中处理此问题? 是否可以捕获并处理此超时操作? 编辑 此外,假设我已完成对SMTP服务器的管理访问,是否可以自行模拟/重现此超时操作?