草庐IT

Try-catch

全部标签

java - 为什么在 Java 的 try-with-resources 构造中 catch 之前调用资源的 close() 方法?

我偶然发现,是这样的。请参阅下面的示例:publicclassAutoClosableTest{publicstaticvoidmain(String[]args)throwsException{try(MyClosableinstance=newMyClosable()){if(true){System.out.println("try");thrownewException("Foo");}}catch(Exceptione){System.out.println("Catched");}finally{System.out.println("Finally");}}publics

java - 有没有更简洁的方法来使用 try-with-resource 和 PreparedStatement?

这是Main.java:packagefoo.sandbox.db;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;publicclassMain{publicstaticvoidmain(String[]args){finalStringSQL="select*fromNVPAIRwherename=?";try(Connectionconnection=DatabaseManager.getConnectio

java - 如何将字符串转换为 float 并避免在 java 中使用 try/catch?

在某些情况下,我需要将字符串转换为float或其他一些数字数据类型,但有可能得到一些不可转换的值,例如“-”或“/”,我无法验证所有值事先删除它们。我想避免在这件事上使用try/catch,有没有其他方法可以在java中进行正确的转换?类似于C#TryParse的东西? 最佳答案 我能想到的最简单的事情是java.util.Scanner。然而,这种方法需要为每个字符串创建一个新的Scanner实例。Stringdata=...;Scannern=newScanner(data);if(n.hasNextInt()){//check

java - 我们能否在发生异常的try block 中获取LineNumber和ColumnNumber

我有以下代码,可以打印发生错误的全类名、类名、方法名。此外,我能够打印行号,但打印的行号是初始化变量“LineNumber”的行。如何在发生错误的tryblock中打印准确的LineNumber和ColumnNumber?try{SQLQuery}catch(Exceptione){StringfullClassName=Thread.currentThread().getStackTrace()[1].getClassName();StringclassName=fullClassName.substring(fullClassName.lastIndexOf(".")+1);Str

java - Java 1.6 中的 Try-with-resources 等价物

我有以下代码:publicclassMain{publicstaticvoidmain(String[]args)throwsSQLException{try(Connectionconn=DBUtil.getConnection(DBType.HSQLDB);Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);ResultSetrs=stmt.executeQuery("SELECT*FROMtours");){DBUtil.getConnec

java - System.exit(1) 并返回

importjava.io.FileNotFoundException;importjava.util.Formatter;importjava.util.FormatterClosedException;importjava.util.NoSuchElementException;importjava.util.Scanner;publicclassCreateTextFile{privateFormatterformatter;publicvoidopenFile(){try{formatter=newFormatter("clients.txt");}catch(Security

java - 捕获多个异常时的特定和相同操作

我想以不同的方式处理两种不同类型的异常,然后对两种异常类型执行一些相同的操作。如何在Java中做到这一点?下面的代码显示了我想做的事情,但它是不正确的,因为一个异常不能被捕获两次。正确的语法是什么?try{//dosomething...}catch(ExceptionAe){//actionsforExceptionA}catch(ExceptionBe){//actionsforExceptionB}catch(ExceptionA|ExceptionBe){//actionsforExceptionA&ExceptionB} 最佳答案

java try catch 异常后还会继续执行吗

1、有try-catch语句块,并且throw在catch语句块里,那么try语句块中引发异常(报错)的那一行代码的后续代码都不执行并且catch语句块后的代码也都不执行(遇到finally除外)。(见情形一和情形二)2、有try-catch语句块,并且throw在try语句块里,那么try语句块中引发异常(报错)的那一行代码的后续代码都不执行,但是catch语句块后的代码会继续执行。(见情形三)3、有try-catch语句块,但是没有throw语句,那么try语句块中引发异常(报错)的那一行代码的后续代码都不执行,但是catch语句块后的代码会继续执行。(见情形四)4、有try-catch语

java - 在一个 catch block 中使用多个异常有什么好处?

我们都听说在Java7中我们可以这样写:try{//somethingwithfilesandIO}catch(FileNotFoundException|IOExceptionex){ex.printStackTrace();System.out.println("It'scan'tcopyfile");}代替try{//somethingwithfilesandIO}catch(FileNotFoundExceptionwx){ex.printStackTrace();}catch(IOExceptionex){ex.printStackTrace();}但是,除了更短的代码之外,

java - 为什么 try catch 中的 return 语句与 'throws' 一起工作

不起作用(编译错误:缺少返回语句)publicSqlMapClientTemplategetSqlTempl()throwsUivException,SQLException{try{SqlMapClientscl=(SqlMapClient)ApplicationInitializer.getApplicationContext().getBean("MySqlMapClient");DataSourcedsc=(DataSource)ServiceLocator.getInstance().getDataSource(PIH_EIV_ORCL);returnnewSqlMapCli