Tag: try catch

为什么我需要最终使用来关闭资源?

大多数时候,我唯一看到的块块就是这样的 FileInputStream f; try{ f= new FileInputStream(“sample.txt”); //something that uses f and sometimes throws an exception } catch(IOException ex){ /* Handle it somehow */ } finally{ f.close(); } 我的问题是,如果f的范围以封闭块结束,为什么我们需要在finally中关闭它?

尝试Catch Performance Java

捕获exception而不是进行检查时,try-catch需要多长时间(以纳秒为单位)(假设消息具有HashMap类型的查找性能)? try { timestamp = message.getLongField( MessageField.TIMESTAMP ); } catch (MissingDataException e) { //Not all messages contain this field } VS if (message.contains(MessageField.TIMESTAMP)) timestamp = message.getLongField( MessageField.TIMESTAMP );

在预期时处理NumberFormatException的正确方法是什么?

我遇到了这种情况,我需要将一个String解析为一个int ,我不知道如何处理NumberFormatException 。 当我没有抓住它时,编译器不会抱怨,但我只是想确保我正确处理这种情况。 private int getCurrentPieceAsInt() { int i = 0; try { i = Integer.parseInt(this.getCurrentPiece()); } catch (NumberFormatException e) { i = 0; } return i; } 我想简化这样的代码。 编译器没有问题,但线程在NumberFormatException上死亡。 private int getCurrentPieceAsInt() { int i = 0; i = Integer.parseInt(this.getCurrentPiece()); return i; } Google CodePro希望我以某种方式记录exception,我同意这是最佳做法。 private int getCurrentPieceAsInt() { int i = 0; […]

java:尝试finally块执行

当存在return;时,我对try-finally执行感到困惑return; 在try块中。 根据我的理解,finally块将始终执行,即在返回调用方法之前。 在考虑以下简单代码时: public class TryCatchTest { public static void main(String[] args){ System.out.println(test()); } static int test(){ int x = 1; try{ return x; } finally{ x = x + 1; } } } 打印的结果实际为1.这是否表示finally块未执行? 任何人都可以帮助我吗?

为什么Java的try-with-resource需要声明

Java7的try-with-resources非常棒,但是我无法理解为什么需要在try语句中包含资源声明。 我的直觉说以下应该是可能的: CloseableResource thing; try (thing = methodThatCreatesAThingAndDoesSomeSideEffect()) { // do some interesting things } thing.collectSomeStats(); 唉,这会导致语法错误(密码期待a ; )。 将类型定义/声明移动到try语句中,这当然会将事物移动到相应的范围中。 我可以弄清楚如何解决这个问题,当我想要更多来自我的AutoClosable不是关闭时,我很感兴趣为什么编译器需要它这样。

捕获嵌套到另一个exception中的exception

我想捕获一个exception,它嵌套到另一个exception中。 我这样做是这样的: } catch (RemoteAccessException e) { if (e != null && e.getCause() != null && e.getCause().getCause() != null) { MyException etrp = (MyException) e.getCause().getCause(); … } else { throw new IllegalStateException(“Error at calling service ‘service'”); } } 有没有办法更有效和优雅地做到这一点?

什么是Java 7 try-with-resources字节码等效使用try-catch-finally?

我试图通过使用常规的try-catch-finally语句重新创建它来了解新的try-with-resources语句是如何工作的。 给定以下使用Java 7 try-with-resources的测试类: import java.io.IOException; import java.util.zip.GZIPOutputStream; public class TryWithResources { public static void main(String[] args) { try (GZIPOutputStream gzip = new GZIPOutputStream(System.out)) { gzip.write(“TEST”.getBytes(“UTF-8”)); } catch (IOException ioe) { ioe.printStackTrace(); } } } 你会如何重写这个类来使用try-catch-finally语句,它产生与try-with-resources语句产生完全相同的字节码? 此外,使用两个资源时的问题相同,如下例所示: import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPOutputStream; public class TryWithResources2 { public static void main(String[] args) { try (ByteArrayOutputStream […]

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

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,则只能访问某些代码。 是否有任何可能发生这种情况的情况? […]

尝试使用Java 1.6中的资源

我有以下代码: public class Main { public static void main(String[] args) throws SQLException { try ( Connection conn = DBUtil.getConnection(DBType.HSQLDB); Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery(“SELECT * FROM tours”); ) { DBUtil.getConnection(); } catch (SQLException e) { DBUtil.processException(e); } } } 我使用此代码从数据库中获取数据。 我的问题是我不允许使用Java 1.7编译器并且必须使用1.6。 如何将try-with-resources-code转换为与1.6编译器一起使用? 在这个特殊的尝试块中究竟发生了什么?

finally {}块不会执行的情况是什么?

在Java try{} … catch{} … finally{}块中, try{} … catch{} … finally{}代码通常被认为是“保证”运行,无论try / catch中发生了什么。 但是,我知道至少有两种情况不会执行: 如果调用System.exit(0) ; 要么, 如果exception被抛出到JVM并且发生默认行为(即printStackTrace()并退出) 是否有任何其他程序行为会阻止finally{}块中的代码执行? 代码在什么具体条件下执行? 编辑:正如NullUserException指出的那样,第二种情况实际上并非如此。 我认为这是因为标准错误中的文本在标准输出之后打印出来,防止文本在没有向上滚动的情况下被看到。 :) 道歉。