Tag: exception处理

如何使用AspectJ拦截处理自身exception的方法

我正在尝试在发生某些特定exception时添加一些监控。 例如,如果我有这样的方面: @Aspect public class LogAspect { @AfterThrowing(value = “execution(* *(..))”, throwing = “e”) public void log(JoinPoint joinPoint, Throwable e){ System.out.println(“Some logging stuff”); } } 和测试类: public class Example { public void divideByZeroWithCatch(){ try{ int a = 5/0; } catch (ArithmeticException e){ System.out.println(“Can not divide by zero”); } } public void divideByZeroWithNoCatch(){ int b = […]

程序打印1-100素数并在给定范围内抛出复合数的exception

我制作了一个打印1-100个素数的程序。 请帮我在1到100个数字的范围内抛出复合数的exception。 我是初学者,所以任何帮助将不胜感激。 public static void main(String[] args) { System.out.println(“Prime numbers from 1 – 100 are :”); int i = 0; int x = 0; for (i = 1; i = 1; x–) { if (i % x == 0) { ctr = ctr + 1; } } if (ctr == 2) { System.out.println(i); } […]

Selenium偶尔会出现UnreachableBrowserException

我试图通过在Java中使用Selenium来访问多个网站。 偶尔,我得到一个UnreachableBrowserException 。 我已经读过很多关于这个错误的线索,但似乎有很多不同的错误原因。 当我尝试访问新页面时,我得到的错误大约是1%的时间,我发现事件之间没有任何相似之处。 我目前正在使用Firefox,但我也尝试过Internet Explorer并遇到过类似的错误。 我一次只打开一个页面并尝试使用相同的窗口并在尝试访问另一个页面之前完全退出驱动程序,无论哪种方式仍然出现错误。 重要的是要注意我并不总是得到这个错误有时我的代码可以运行而不会发生这种情况。 这是错误消息: Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute INFO: I/O exception (java.net.SocketException) caught when processing request to {}- http://127.0.0.1:7055: Permission denied: connect Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute INFO: Retrying request to {}->http://127.0.0.1:7055 Jan 12, 2015 10:39:40 PM org.apache.http.impl.execchain.RetryExec execute INFO: I/O exception (java.net.SocketException) caught […]

Java中的Processbuilder不会抛出子流程exception

我正在尝试使用ProcessBuilder在Java中执行jar文件。 现在,每当ProcessBuilder调用的子进程抛出任何exception时,ProcessBuilder都不会捕获exception并且执行会继续继续进行。 以下是我的代码: try { ProcessBuilder pb = new ProcessBuilder(“java”, “-jar”, CommonConstants.jarFileLocation, fileEntry.getAbsolutePath(), CommonConstants.commonFileLocation); Process p = pb.start(); } catch (Exception e) { e.printStackTrace(); } catch块假设在子进程抛出任何exception时打印任何exception。 然而,它永远不会。 我错过了什么吗?

当序列化策略更改时,GWT客户端不会收到IncompatibleRemoteServiceException

部署新版本的应用程序时,会更改模型类(例如添加/删除字段)。 运行旧版本的客户端将获得com.google.gwt.user.client.rpc.SerializationException,并使用旧客户端代码生成RPC,这是预期的行为。 因此,我们希望在客户端看到IncompatibleRemoteServiceException。 但是我们得到StatusCodeException。 StatusCodeException是500错误,我们无法轻松自定义客户端行为(我们不希望假设每个StatusCodeException或500错误是新版本)。 我们在这里做错了什么? 注意:在服务器端(日志),我们显然得到SerializationExcepion,因为来自旧客户端的序列化策略对新服务器不再有效。 那么为什么不抛出IncompatibleRemoteServiceException呢? 谢谢。

Java编译器抱怨未报告的IOException

我正在尝试编写一个列出目录中所有非隐藏文件的方法。 但是,当我添加条件!Files.isHidden(filePath)我的代码将无法编译,并且编译器返回以下错误: java.lang.RuntimeException: Uncompilable source code – unreported exception java.io.IOException; must be caught or declared to be thrown 我试图捕获IOException ,但编译器仍然拒绝编译我的代码。 有什么明显的东西让我失踪吗? 代码如下。 try { Files.walk(Paths.get(root)).forEach(filePath -> { if (Files.isRegularFile(filePath) && !Files.isHidden(filePath)) { System.out.println(filePath); } }); } catch(IOException ex) { ex.printStackTrace(); } catch(Exception ex) { ex.printStackTrace(); }

如何在我的自定义exception中设置我自己的消息,可以检索我的getMessage()但是没有使用构造函数,有什么办法吗?

我刚学习Java中的exception处理。 我想知道的不是尝试说: throw new Exception(“My Message”); 和 String message=ex.getMessage(); System.out.println(message); 看看下面的代码, class ExceptionTest { public static void main(String[] args) { ExceptionTest t1=new ExceptionTest(); try { t1.riskyMethod();//call the risky or exception throwing method } catch(MyException myex) { System.out.println(“Exception has been thrown”); String message=myex.getMessage();//get the String passed during exception call System.out.println(“The message retrieved is “+message); myex.printStackTrace();//prints name […]

捕获Java中的exception

Java中存在某些预定义的exception,如果抛出这些exception,则会报告发生了严重的事情并且您可以更好地改进代码,而不是在catch块中捕获它们(如果我已正确理解它)。 但是我仍然发现许多程序,其中包含以下内容: } catch (IOException e) { … } catch (FileNotFoundException e) { …. } 我认为IOException和FileNotFoundException正是这种exception,我们不应该在catch块中捕获它们。 为什么人们这样做? 这样抓住他们会更好吗? 无论如何,Java编译器都会警告这种问题。 谢谢。

如何在scheduleWithFixedDelay抛出exception时重新启动计划?

我使用ScheduledExecutorService来安排一些需要定期运行的任务。 我想知道这个代码是否可以在发生exception时恢复计划。 ScheduledExecutorService service = Executors.newScheduledThreadPool(1); this.startMemoryUpdateSchedule(service);//See below method //Recursive method to handle exception when run schedule task private void startMemoryUpdateSchedule(ScheduledExecutorService service) { ScheduledFuture future = service.scheduleWithFixedDelay(new MemoryUpdateThread(), 1, UPDATE_MEMORY_SCHEDULE, TimeUnit.MINUTES); try { future.get(); } catch (ExecutionException e) { e.printStackTrace(); logger.error(“Exception thrown for thread”,e); future.cancel(true); this.startMemoryUpdateSchedule(service); } catch(Exception e) { logger.error(“Other exception “,e); } }

为日志记录目的捕获RuntimeException是不好的做法吗?

我发现捕获RuntimeException通常被认为是不好的做法,因为它们无法纠正,通常是程序员错误。 但是,我们有一个(疯狂的)大型应用程序,任何部分的更改都可能产生无法预料的后果(是的,这本身就是一个问题) 。 现在,我们开始在应用程序的顶层开始捕获和记录RuntimeExceptions,这样我们就可以更有效地修复出现这样的流失问题。 像每个优秀的Java团队一样,我们有一个可爱的热心叔叔鲍勃粉丝,绝对禁止我们这样做。 这样做有多糟糕? 真的没有这种情况可以接受甚至推荐吗?