标签: try-catch-finally

try-catch-finally在Java问题中抛出异常

我是Java初学者,但我认为在使用try-catch-finally时我不必使用声明异常throws SQLException.但是,如果我不使用它,编译器会给我错误:

"未报告的异常java.sql.SQLException;必须被捕获或声明被抛出".

我包含了一个catch,所以我不确定为什么会出现这种错误.

public static ResultSet getResultSet ( String query ) 
{
    dbConn = getConnection();

    try
    {
       stmt = dbConn.createStatement( );

       ResultSet rs = stmt.executeQuery( query );

       return rs;
    }
   catch (SQLException ex)
   {
       return null;
   }
   finally
   {
       stmt.close();
       dbConn.close();
   }
}
Run Code Online (Sandbox Code Playgroud)

java exception try-catch-finally

0
推荐指数
1
解决办法
2696
查看次数

java:异常:总是到达终点?

可能重复:
finally块是否始终运行?

让我们想象以下场景:

public void myMethod() throws MyException
  try
  {
     // do something
     // an Exception (for example an individual written MyException which extends 
     // "Exception" is thrown here
  }
  catch (OtherException e)
  {
    // do something
  }
  finally 
  {
    // do something else
  }
}
Run Code Online (Sandbox Code Playgroud)

如果在try块中抛出"MyException"并且不会被捕获 - 它们最终会被阻止,但是,对吗?

如果它是一个运行时异常会被抛出怎么办?最终块会到达吗?

有没有无法到达finally块的情况?

谢谢你的回答:-)

java exception-handling exception try-catch-finally runtimeexception

0
推荐指数
1
解决办法
1499
查看次数

我不会在最后的blcok中使用try-catch语句.(Java)的

这是一些java代码.

public class SomeClass {
private Connection connection;

public SomeClass(Connection c) {
    connection = c;
}
public void someWork(){
    Connection c;
    try {
        // do something
    } catch (Exception e) {
        // some exception code
    } finally {
        if (conn != null){
            try {c.close();} catch (Exception e) {}
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

}

但我不喜欢代码

if (conn != null){
        try {c.close();} catch (Exception e) {}
    }
Run Code Online (Sandbox Code Playgroud)

所以我认为代码

...catch (Exception e) {
        // some exception code
    } finally {
        c = null;
    } …
Run Code Online (Sandbox Code Playgroud)

java connection garbage-collection stream try-catch-finally

0
推荐指数
1
解决办法
266
查看次数

Try-Catch-Finally执行顺序

这是演示我的观点的代码(java):

public static int getSize(List<String> list)
{
    System.out.println("begin");
    try
    {
        System.out.println("get list size");
        return list.size();
    }
    catch (Exception e)
    {
        System.err.println("exception");
    }
    finally
    {
        System.out.println("finally");
        return -1;
    }
}

public static void main(String[] args) 
{
    List<String> list = null;
    int size = getSize(list);
    System.out.println("list size: " + size);
}
Run Code Online (Sandbox Code Playgroud)

我期望输出:

begin
get list size
exception
finally
list size: -1
Run Code Online (Sandbox Code Playgroud)

相反,我得到其他东西和上面的那个...就像异常可以在获取列表大小之前和异常之后显示...

例如

begin
exception
get list size
finally
list size: -1
Run Code Online (Sandbox Code Playgroud)

begin
get list size
finally
exception
list size: -1 …
Run Code Online (Sandbox Code Playgroud)

java execution try-catch-finally

0
推荐指数
1
解决办法
320
查看次数

C# - 在try块外定义var并返回它

我是一个新手,并试图回到编程游戏.对不起我的无知和缺乏知识.

我试图在下面的代码中看到如何修复返回类型错误消息.我知道我可以使用显式数据类型在try块之外定义变量,但可以为'var'或任何其他建议完成.

private IEnumerable GetDirFiles(String location)
{
    try
    {
        //Search all directories for txt files
        var emailfiles = Directory.EnumerateFiles(location, "*.txt", SearchOption.AllDirectories);
    }
    catch(Exception ex)
    {
        Console.WriteLine("Message for admins: " + ex.Message);
    }
    finally
    {
        textBox1.Clear();
        var emailfiles = Directory.EnumerateFiles(location, "*.msg", SearchOption.AllDirectories);
    }

    return emailfiles;
}
Run Code Online (Sandbox Code Playgroud)

错误消息是"当前上下文中不存在emailfiles",我理解为什么因为它是在try块中定义的.

谢谢.

c# scope var try-catch-finally

0
推荐指数
1
解决办法
99
查看次数

了解 Try/Finally 抑制的异常

我正在学习例外,我发现你可以有压抑的例外。我在 stackoverflow 上读了很多例子,但它们仍然不能像在“try/finally”情况下那样工作:

public class MultipleExceptionsExample {

   static class IOManip implements Closeable{
       @Override
       public void close() {
           throw new RuntimeException("from IOManip.close");
       }
   }

   public static void main(String[] args) {
       try(IOManip ioManip = new IOManip()){
           throw new RuntimeException("from try!");
       }catch(Exception e){
           throw new RuntimeException("from catch!");
       }finally{
           throw new RuntimeException("from finally!");
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

正如许多人所解释的,我应该得到所有行:“java.lang.RuntimeException:来自finally!” (是的,我愿意)

删除finally块我应该得到:“java.lang.RuntimeException:来自catch!” (是的,我愿意)

删除 catch 块我应该得到:

Exception in thread "main" java.lang.RuntimeException: from try!
    Suppressed: java.lang.RuntimeException: from IOManip.close
Run Code Online (Sandbox Code Playgroud)

我从来不这样做!为什么?我缺少什么?

通过删除 catch 块,我应该看到 try 消息,但我得到的是:

Exception in thread "main" …
Run Code Online (Sandbox Code Playgroud)

java exception try-catch try-catch-finally

0
推荐指数
1
解决办法
424
查看次数

try-catch-finally执行顺序

我在理解try-catch-finally的执行顺序时遇到了问题.我见过的所有例子(如:http://stackoverflow.com/questions/4191027/order-of-execution-of-try-catch-and-finally-block)都有一个非常简单的"捕获"部分,打印到控制台.但如果我在捕获中使用"throw"语句会发生什么?

我能想到的最简单的代码可以捕获问题:

public class TestClass
{
    void Foo(int num)
    {
        int answer = 100;
        try
        {
            answer = 100 / num;
        }
        catch (Exception e)
        {
            //Probably num is 0
            answer = 200;
            throw;
        }
        finally
        {
            Console.WriteLine("The answer is: " + answer);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果num == 2,那么输出将是:

答案是:50

但是会为num == 0打印什么?

答案是:100
答案是:200根本
没有印刷......

还是仅仅是一种"未定义的行为"?

c# try-catch-finally

-2
推荐指数
1
解决办法
1628
查看次数

eclipse警告:最后块无法正常完成

我有一些代码会抛出一个潜在的错误,我会抓住这些,然后跳进一个finally块,这里是'伪'形式.

private boolean myMethod(ResultSet rs1, ResultSet rs2){

Vector<String> temp = new Vector<String>();

try{

//add info into temp while condition is true
while(true){temp.add(information);}//potentially throws an SQL error

//test my temo object for some codition
if(false){throw InternalError();}


}catch (SQLException e){
     //send error message
}
catch (InternalError e){
     //send error message
}

finally{
//whatever happens I need to send a result of some sort!

if(temp.size() > 1){
   //create class member variable from this info
}

//send a message explaining that an …
Run Code Online (Sandbox Code Playgroud)

java eclipse try-catch-finally

-3
推荐指数
2
解决办法
3752
查看次数

最终使用是一种好习惯吗

我们在代码中使用try catch块。我想问的是,使用finally块是一种好习惯。我还没看到多少最终会阻塞代码。这是不好的做法吗?

java exception finally try-catch try-catch-finally

-3
推荐指数
1
解决办法
812
查看次数

-5
推荐指数
1
解决办法
309
查看次数