Results 1 to 6 of 6
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Unable to get out of catch block after printing stacktrace for one time

    I have a try-catch block as defined below and a for-each loop inside it.


    Java Code:
        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 again and again. I want to move onto other connections.

    Is there a way I can exit the catch block quickly just after printing the stack trace once and then move on to other connections?

    Some of the approaches I have tried are below:

    Approach #1:

    Java Code:
    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
    RESULT of above approach: It keeps on printing Value of the IP Address Field:11.11.1.111 and then stacktrace again and again


    Second Approach I have tried:

    Java Code:
    // Doing some JDBC Connections here
    
    
    
    
    
           Map<String,Connection> connections = new HashMap<>();
            try
            {
    
               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));
               }  
        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
    RESULT of above approach: my application just exits after printing stacktrace for one time which I don't want. I want to print the stack trace once and then move onto other connections


    Could anyone please help me and tell me what I am doing wrong? Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Unable to get out of catch block after printing stacktrace for one time

    That first bit of code has no catch block inside either of those loops, so cannot be called multiple times (based on what you have shown here).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: Unable to get out of catch block after printing stacktrace for one time

    Where exactly do you think I should have the catch block in the first code (Code mentioned above Approach #1)?
    Quote Originally Posted by Tolls View Post
    That first bit of code has no catch block inside either of those loops, so cannot be called multiple times (based on what you have shown here).

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,029
    Rep Power
    6

    Default Re: Unable to get out of catch block after printing stacktrace for one time

    You say:

    Quote Originally Posted by Jack_Tauson_Sr View Post
    So, if something goes wrong with the connection, my program will get stuck in the catch block,printing the stack trace again and again.
    Yet your code says:
    Java Code:
        try
        {
      
         // Doing some JDBC Connections here. Now if something goes wrong with the connection here...
        ...
    
    
        }// ENd of try block
        catch(SQLException ex){
    	
    	ex.printStackTrace();    // ... the exception is caught here, OUTSIDE of the loops you have.
    	
    	}

    The code breaks out of either loop if an exception happens, so it cannot endlessly loop. You probably missed some code.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Unable to get out of catch block after printing stacktrace for one time

    Quote Originally Posted by Jack_Tauson_Sr View Post
    Where exactly do you think I should have the catch block in the first code (Code mentioned above Approach #1)?
    From your OP:
    "I want to print the stack trace once and then move onto other connections."

    So the try/catch needs to be inside the while loop.

    Java Code:
    while device results
       try
          create connection and add to list
       catch
          log and move on
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: Unable to get out of catch block after printing stacktrace for one time

    Thanks a lot Tolls. After long time, I finally achieved for my goal. Thanks for your help. Appreciated.
    Quote Originally Posted by Tolls View Post
    From your OP:
    "I want to print the stack trace once and then move onto other connections."

    So the try/catch needs to be inside the while loop.

    Java Code:
    while device results
       try
          create connection and add to list
       catch
          log and move on

Similar Threads

  1. Try Catch Block
    By Jackbower in forum New To Java
    Replies: 3
    Last Post: 05-06-2012, 07:00 AM
  2. executing catch block by saying unable to connect to database
    By sharanya in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 02-18-2011, 10:27 AM
  3. Try/catch block
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-02-2009, 02:32 PM
  4. Question reg try/catch block
    By nn12 in forum New To Java
    Replies: 1
    Last Post: 09-16-2008, 05:56 PM
  5. try...catch block
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •