Java 从catch内以及finally块引发异常
我想抛出对应用程序执行MySQL事务时发生的任何异常。但在此之前,我想关闭任何处于开放状态的资源。但关闭这些资源可能会再次生成异常,我希望再次向应用程序报告该异常。 下面的代码将使这一点更加清楚:Java 从catch内以及finally块引发异常,java,try-catch,finally,Java,Try Catch,Finally,我想抛出对应用程序执行MySQL事务时发生的任何异常。但在此之前,我想关闭任何处于开放状态的资源。但关闭这些资源可能会再次生成异常,我希望再次向应用程序报告该异常。 下面的代码将使这一点更加清楚: try { // connect to MySQL DB using JDBC and run different queries } catch ( Exception e ) { // throw this exception by wrapping it in another use
try
{
// connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
// throw this exception by wrapping it in another user defined exception class
}
finally
{
try
{
// close resources opened in try block ( statement, connection )
}
catch ( Exception e )
{
// throw this exception by wrapping it in another user defined exception class
}
}
我想知道处理这种情况的正确方法是什么(抛出两个异常)。
谢谢您的帮助。您可以这样尝试:
try
{
// connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
// throw this exception by wrapping it in another user defined exception class
}
finally
{
closeMyConnection(resource);
}
protected void closeMyConnection( Resource resource ) {
try {
if (resource != null) {
resource.close();
}
} catch( Exception ex ) {
log( "There is some excetion", ex );
}
}
您可以这样尝试:
try
{
// connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
// throw this exception by wrapping it in another user defined exception class
}
finally
{
closeMyConnection(resource);
}
protected void closeMyConnection( Resource resource ) {
try {
if (resource != null) {
resource.close();
}
} catch( Exception ex ) {
log( "There is some excetion", ex );
}
}
您可以这样尝试:
try
{
// connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
// throw this exception by wrapping it in another user defined exception class
}
finally
{
closeMyConnection(resource);
}
protected void closeMyConnection( Resource resource ) {
try {
if (resource != null) {
resource.close();
}
} catch( Exception ex ) {
log( "There is some excetion", ex );
}
}
您可以这样尝试:
try
{
// connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
// throw this exception by wrapping it in another user defined exception class
}
finally
{
closeMyConnection(resource);
}
protected void closeMyConnection( Resource resource ) {
try {
if (resource != null) {
resource.close();
}
} catch( Exception ex ) {
log( "There is some excetion", ex );
}
}
我建议你使用
在Oracle文档中对其进行了更好的解释
示例代码:
try( Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery() ) {
// connection, statements and result set are automatically closed
}
注意:try with resources语句可以像普通的try
语句一样具有catch
和finally
块。在try-with-resources语句中,任何catch
或finally
块都会在声明的资源关闭后运行
在Oracle文档中对其进行了更好的解释
示例代码:
try( Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery() ) {
// connection, statements and result set are automatically closed
}
注意:try with resources语句可以像普通的try
语句一样具有catch
和finally
块。在try-with-resources语句中,任何catch
或finally
块都会在声明的资源关闭后运行
在Oracle文档中对其进行了更好的解释
示例代码:
try( Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery() ) {
// connection, statements and result set are automatically closed
}
注意:try with resources语句可以像普通的try
语句一样具有catch
和finally
块。在try-with-resources语句中,任何catch
或finally
块都会在声明的资源关闭后运行
在Oracle文档中对其进行了更好的解释
示例代码:
try( Connection conn = dataSource.getConnection();
PreparedStatement stmt = conn.prepareStatement(query);
ResultSet rs = stmt.executeQuery() ) {
// connection, statements and result set are automatically closed
}
注意:try with resources语句可以像普通的try
语句一样具有catch
和finally
块。在try-with-resources语句中,在声明的资源被关闭后,运行任何catch
或finally
块
Exception lastException = null:
try
{
// connect to MySQL DB using JDBC and run different queries
}
catch ( Exception e )
{
// throw this exception by wrapping it in another user defined exception class
lastException = e;
}
if (lastException != null) {
// you know what to do
}
试一试
试一试
试一试
您正在使用哪个版本的Java?我正在使用的Java版本是1.6.0\u 22您正在使用哪个版本的Java?我正在使用的Java版本是1.6.0\u 22您正在使用哪个版本的Java?我正在使用的Java版本是1.6.0\u 22您正在使用哪个版本的Java?我正在使用的Java版本是1.6.0\u 22这在我看来是一个很有前途的版本解决方案我们仍然在使用Java6,但我肯定会找到更多关于它的信息。在我看来,这是一个很有前途的解决方案。我们仍然在使用Java6,但我肯定会找到更多关于它的信息。在我看来,这是一个很有前途的解决方案。我们仍然在使用Java6,但我肯定会找到更多关于它的信息。在我看来,这是一个很有前途的解决方案。我们仍然在使用Java6,但我肯定会找到更多关于这方面的信息。感谢R.T.的帮助。基本上,在finally中抛出的任何异常都应该在抛出的地方得到处理,并且原始异常(或任何其他返回值)应该传递回应用程序。我希望这就是大多数应用程序的工作方式。唉,我只能将一个解决方案标记为已接受。谢谢大家的努力和帮助,谢谢R.T.的帮助。基本上,在finally中抛出的任何异常都应该在抛出的地方得到处理,并且原始异常(或任何其他返回值)应该传递回应用程序。我希望这就是大多数应用程序的工作方式。唉,我只能将一个解决方案标记为已接受。谢谢大家的努力和帮助,谢谢R.T.的帮助。基本上,在finally中抛出的任何异常都应该在抛出的地方得到处理,并且原始异常(或任何其他返回值)应该传递回应用程序。我希望这就是大多数应用程序的工作方式。唉,我只能将一个解决方案标记为已接受。谢谢大家的努力和帮助,谢谢R.T.的帮助。基本上,在finally中抛出的任何异常都应该在抛出的地方得到处理,并且原始异常(或任何其他返回值)应该传递回应用程序。我希望这就是大多数应用程序的工作方式。唉,我只能将一个解决方案标记为已接受。但感谢大家的努力和帮助。