Tag: exception处理

在Java中尝试/捕获

有人可以给我一个暗示为什么这个尝试和捕获不起作用? 它会抛出扫描程序exception,而不是打印我期望的消息。 import java.util.*; import java.io.*; import java.math.*; import javax.swing.*; public class Main { public static void main(String[] args) { Boolean test = true; while (test == true) { try { double x, y; String operator; Scanner scan = new Scanner(System.in); Scanner scan_2 = new Scanner(System.in); Scanner ScanOperator = new Scanner(System.in); System.out.println(” Enter a double […]

在Java中抛出exception

我有一个关于在Java中抛出exception的问题,这似乎是我身边的一种误解,我想为自己澄清一下。 我一直在阅读处理exception代码的两种基本方法是: 1.)在一个带有“throw new …”的try-block中抛出exception,然后立即在catch-block中捕获它 – 即所谓的try-throw-catch机制。 2.)在一个带有“throw new …”的方法中抛出一个exception,然后在方法的标题中声明这个方法可能会抛出一个带有“throws …”的exception – 即所谓的pass-the-buck。 我最近读到“抛出一个exception然后用相同的方法捕获它没有任何意义”,这让我想到我是否以错误的方式理解了这个东西,或者写了这个东西的人有什么东西别的想法。 第一种处理exception的方法是不是就是这样(try-throw-catch机制)? 我的意思是,它抛出exception并以相同的方法捕获它。 我已经读过,在一个方法中抛出exception并在另一个方法中捕获它是一个更好的做法,但这只是一种(可能更好)的方法。 另一种方式也是合法和正确的,不是吗? 请你对此发表评论吗? 非常感谢你。

IOException与RuntimeException Java

class Y { public static void main(String[] args) throws RuntimeException{//Line 1 try { doSomething(); } catch (RuntimeException e) { System.out.println(e); } } static void doSomething() throws RuntimeException{ //Line 2 if (Math.random() > 0.5) throw new RuntimeException(); //Line 3 throw new IOException();//Line 4 } } 当我抛出两种类型的exception(Line4中的IOException和Line3中的RunTimeException)时,我发现我的程序在我在第1行和第2行的throws子句中指示“IOException”之前不会编译。 然而,如果我反向“抛出”以指示抛出IOException,程序会成功编译,如下所示。 class Y { public static void main(String[] args) throws […]

如何在junit中处理exception

我写了一些测试用例来测试一些方法。 但是有些方法会抛出exception。 我做得对吗? private void testNumber(String word, int number) { try { assertEquals(word, service.convert(number)); } catch (OutOfRangeNumberException e) { Assert.fail(“Test failed : ” + e.getMessage()); } } @Test public final void testZero() { testNumber(“zero”, 0); } 如果我传递-45 ,它将失败并出现OutOfRangeException但我无法测试特定的exception,如@Test(Expected…)

自定义exception类 – 从Exception或Thowable扩展?

我正在为我的应用程序设计一个自定义的Exception类。 我有一个非常基本的问题。 我应该从Exception类还是Thowable类扩展? 有什么好处? 我打算从底层层中抛出它并在顶层类中捕获它。 它会影响我使用Thowable over Exception的决定吗? 抓住Thowable是否从根本上说是正确的? 我在这个论坛中经历过其他一些话题。 他们谈论在抛出堆栈跟踪时保持堆栈跟踪而没有exception等等。我理解有人说( 这里 )Thowable是Exception的超类,我们不应该使用它。 但是其他人( 这里 )说Exception是针对“例外”的案例。 这个问题相当于讨论一个人如何比其他人更好而不是问如何。

CountDownLatch InterruptedException

我正在使用CountDownLatch来同步两个线程之间的初始化过程,我想知道它可能抛出的InterruptedException的正确处理。 我最初写的代码是这样的: private CountDownLatch initWaitHandle = new CountDownLatch(1); /** * This method will block until the thread has fully initialized, this should only be called from different threads Ensure that the thread has started before this is called. */ public void ensureInitialized() { assert this.isAlive() : “The thread should be started before calling this method.”; […]

为什么捕获RuntimeException不被认为是一个好的编程习惯?

为什么使用catch(Throwable exc) {} RuntimeException不被认为是一个好的编程习惯? 处理RuntimeExceptions的正确方法是什么? 另外,为什么catch(Exception exc) {}没有捕获RuntimeException ? 这个行为是如何实现的?

什么是exception传播?

什么是exception传播 ? 我试过谷歌但不满意结果。 如果可能的话,还请举一些例子来解释。 C ++,php和java语言更可取。

番石榴缓存并保留已检查的exception

我正在重构一些使用番石榴缓存的代码。 初始代码: public Post getPost(Integer key) throws SQLException, IOException { return PostsDB.findPostByID(key); } 为了不破坏某些东西,我需要保留任何抛出的exception,而不包装它。 目前的解决方案看起来有些难看 public Post getPost(final Integer key) throws SQLException, IOException { try { return cache.get(key, new Callable() { @Override public Post call() throws Exception { return PostsDB.findPostByID(key); } }); } catch (ExecutionException e) { Throwable cause = e.getCause(); if (cause instanceof SQLException) […]

不能使用try / catch块处理java未经检查的exception?

在一个教程中,我发现你的代码无法处理Unchecked Exception,即我们不能使用try/catch块,例子是ArrayIndexOutOfBoundsException, NullPointerException.等exceptionArrayIndexOutOfBoundsException, NullPointerException. 但是这些exception可以使用try / catch块来处理。 我想我不清楚这个概念!! 另外我认为throw关键字只能用于try/catch block.can throw关键字与UncheckedException一起使用?