Tag: exception处理

在Java中的Rethrowexception

关于在Java中重新抛出exception,我有一个非常简单的问题。 这是代码片段: public static void main(String[] args) throws FileNotFoundException { try { FileReader reader = new FileReader(“java.pdf”); } catch (FileNotFoundException ex) { throw ex; } } public static void main(String[] args) throws FileNotFoundException { FileReader reader = new FileReader(“java.pdf”); } 为什么我们需要在第一个版本中重新抛出ex ,而第二个版本看起来更优雅? 什么可能是好处,哪个版本比另一个更受欢迎?

如何在Java中捕获AWT线程exception?

我们想在我们的应用程序日志中跟踪这些exception – 默认情况下,Java只是将它们输出到控制台。

使用CompletableFuture处理Java 8供应商exception

请考虑以下代码 – public class TestCompletableFuture { BiConsumer biConsumer = (x,y) -> { System.out.println(x); System.out.println(y); }; public static void main(String args[]) { TestCompletableFuture testF = new TestCompletableFuture(); testF.start(); } public void start() { Supplier numberSupplier = new Supplier() { @Override public Integer get() { return SupplyNumbers.sendNumbers(); } }; CompletableFuture testFuture = CompletableFuture.supplyAsync(numberSupplier).whenComplete(biConsumer); } } class SupplyNumbers […]

Java:处理子线程中的exception

我更喜欢在main方法附近的调用堆栈中进一步使用exception处理逻辑。 我喜欢这种方法……但是,我创建了一个线程,其中run()内部的一些方法调用可能会抛出exception。 我真的想看看是否有一种方法可以将这些exception抛回到父线程? 我能想到的最好的方法是在实现Runnable的对象中设置一个变量。 此变量是一个包含错误消息的字符串,然后使用类加载器在父线程中正确地重新创建相同的exception。 我想知道的是,是否有一种不那么混乱的方式来获得我想要的东西? (为了能够确保在子线程中抛出的任何exception都使用相同的exception处理逻辑来处理,就好像它在主线程/代码重用中运行一样)。

Java中是否有未处理的exception处理程序?

如果我在.NET中正确记得,可以为未处理的exception注册“全局”处理程序。 我想知道Java是否有类似的东西。

处理ThreadPoolExecutor的exception

我有以下代码片段,它基本上扫描需要执行的任务列表,然后将每个任务提供给执行程序执行。 JobExecutor反过来创建另一个执行程序(用于执行数据库内容…读取和写入数据到队列)并完成任务。 JobExecutor为提交的任务返回Future 。 当其中一个任务失败时,我想优雅地中断所有线程并通过捕获所有exception来关闭执行程序。 我需要做哪些改变? public class DataMovingClass { private static final AtomicInteger uniqueId = new AtomicInteger(0); private static final ThreadLocal uniqueNumber = new IDGenerator(); ThreadPoolExecutor threadPoolExecutor = null ; private List sources = new ArrayList(); private static class IDGenerator extends ThreadLocal { @Override public Integer get() { return uniqueId.incrementAndGet(); } } public void […]

JavaFx 2.x – Swing:不在FX应用程序线程上

我试图通过使用附加JFXPanel的JInternalFrame来使用JavaFx 2.x和Swing应用程序 我的代码如下 public class InternalFrameWithJavafx extends javax.swing.JFrame { /** * Creates new form InternalFrameWithJavafx */ public InternalFrameWithJavafx() { initComponents(); final JInternalFrame frame = new JInternalFrame(); frame.setTitle(“test InternalFrame”); frame.setVisible(true); frame.setResizable(true); frame.setIconifiable(true); frame.setMaximizable(true); frame.setIconifiable(true); frame.setClosable(true); frame.setSize(800,600); frame.setLocation(0, 0); frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { final JFXPanel javafxPanel = new JFXPanel(); BorderPane pane = […]

在Java中,是使用throws Exception而不是抛出多个特定exception的好习惯?

在查看Spring MVC框架时,我注意到,除非我误解,否则它的开发人员会抛出抛出Exception而不是抛出多个exception。 我意识到这个问题的核心是检查与未检查的exception辩论,避免宗教战争,使用抛出一般exception是一种好的做法吗?

如何在Swift中向堆栈跟踪传递错误

在java中,如果一个方法抛出错误,则调用它的方法可以将其传递给下一个方法。 public void foo() throws Exception { throw new Exception(); } public void bar() throws Exception { foo(); } public static void main(String args[]) { try { bar(); } catch(Exception e) { System.out.println(“Error”); } } 我正在写一个快速的应用程序,并希望做同样的事情。 这可能吗? 如果不可能有什么其他可能的解决方案? 我调用此调用的原始函数具有此结构。 func convert(name: String) throws -> String { }

尝试用资源引入无法访问的字节码

javac是否有可能为以下过程生成无法访问的字节码? public void ex06(String name) throws Exception { File config = new File(name); try (FileOutputStream fos = new FileOutputStream(config); PrintWriter writer = new PrintWriter(new OutputStreamWriter( fos , “rw”))) { bar(); } } 当我查看字节码的exception表(javap -v)时,有以下条目看起来很奇怪: 43 48 86 Class java/lang/Throwable 43 48 95 any 和 21 135 170 Class java/lang/Throwable 21 135 179 any 现在的问题是,如果捕获了类型为“any”而不是Throwable的exception,则只能访问某些代码。 是否有任何可能发生这种情况的情况? […]