简体   繁体   English

迅速摆脱困境

[英]quicky getting out of the catch block

I have a try-catch block as defined below and a for-each loop inside it. 我有一个下面定义的try-catch块,里面有一个for-each循环。

try
{

 // Doing some JDBC Connections here

  Map<String,Connection> connections = new HashMap<>();

   while(DeviceRS.next()){
   final String ip_address = DeviceRS.getString("Connection_vch");
       System.out.println("Value of the IP Address Field:"+ip_address);
   connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));
   }    

    for(final String ip : connections.keySet())
    {

      // Selecting and inserting into database here ...

     }// ENd of for loop

}// ENd of try block
catch(SQLException ex){

ex.printStackTrace();

}

So, if something goes wrong with the connection, my program will get stuck in the catch block,printing the stack trace. 因此,如果连接出现问题,我的程序将卡在catch块中,并打印堆栈跟踪。 I want to move onto other connections. 我想转到其他连接。

Is there a way can exit the catch block quickly just after printing the stack trace? 有没有一种方法可以在打印堆栈跟踪后立即快速退出catch块?

Note: I haven't mentioned full code here as I think my question is not concerned with my code. 注意:我在这里没有提到完整的代码,因为我认为我的问题与代码无关。

Changed your code like this. 像这样更改您的代码。 To quickly skip it on its failure. 快速跳过它的失败。

 // Doing some JDBC Connections here





       Map<String,Connection> connections = new HashMap<>();
        try
        {

           while(DeviceRS.next()){
           try{
           final String ip_address = DeviceRS.getString("Connection_vch");
           connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));
            }
            catch(SQLException ex){
            ex.printStackTrace();

             }
           }  
    catch(Exception ex){

        ex.printStackTrace();
            System.out.println("in catch block");
//System.exit(0); // dont use this -in real time projects
        }finally{
       System.out.println("in finally block");

        } 

            for(final String ip : connections.keySet())
            {

              // Selecting and inserting into database here ...

             }// ENd of for loop

        }// ENd of try block

I don't think there's fine looking way. 我认为没有好的方法。 But you can make another try-catch block inside your try-catch 但是您可以在try-catch中创建另一个try-catch块

 try {
     // your code here

     for(final String ip : connections.keySet()) {
         try { // inner catch
             // your code, that will be continued if sql exception occurs
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }

 } catch (SQLException e) {
     e.printStackTrace();
 }

Like you said if you want if something goes wrong with the connection and you want to move onto another connection,just do this : 就像您说的那样,如果您想要连接出现问题并且想要转到另一个连接,只需执行以下操作:

 boolean check = true;

 while(check) {
   try {

   Map<String,Connection> connections = new HashMap<>();

   while(DeviceRS.next()){

   final String ip_address = DeviceRS.getString("Connection_vch");
   connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address   +    ":3306/test",RemoteUser,RemotePass));

  } // Ends inner while    

  check = false;  //As connection is ok,we don't need to loop back.  

   for(final String ip : connections.keySet())
   {

     // Selecting and inserting into database here ...
   }

 }// Ends try block

 catch(SQLException ex){

    ex.printStackTrace();

   } //Ends catch block

  finally{

         // close the connection if needed.
   } // Ends finally block

}// Ends outer while loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM