Robert Strauch
Robert Strauch

Reputation: 12896

Continue after try-catch-finally

This might sound like a weird question but I don't get it...

Let's say I have an application which connects to a server to do some stuff. This connect might fail and throw an exception which I can catch.

try {
  Client.connect();
} catch (System.Exception ex) {
  // Do some exception handling...
} finally {
  // Do some cleanup...
}

However, in case that the connect is succcesful the application shall continue...

try {
  Client.connect();
} catch (System.Exception ex) {
  // Do some exception handling...
} finally {
  // Do some cleanup...
}

// Talk to the server...

The "server talking" however is executed in any case. It doesn't matter if the exception occured or not.

How can I make sure that the "server talking" is only executed if the connect was successful? Do I have to move all of the following code inside the trystatement? What is a clean way to program such a behavior?

Upvotes: 5

Views: 1575

Answers (5)

Eren Ersönmez
Eren Ersönmez

Reputation: 39085

Have another variable like clientConnected and set it to true right after Client.Connect(). Then outside the try-catch check for clientConnected before talking to the server.

Avoid doing everything in a single try-catch. You should use separate try-catch blocks for different actions that might throw exceptions, and catch specific exceptions as much as possible.

Upvotes: 2

Zlatan
Zlatan

Reputation: 698

Use some type of flag variable to indicate whether server is connected or not. If your method is returning a boolean variable then also it is ok.

int flag=0;
while(flag==0){
   try {
       Client.connect();
       flag=1;
   } catch (System.Exception ex) {
   // Do some exception handling...
   } finally {
      // Do some cleanup...
   }
}

//If server connects code

Upvotes: 0

JDB
JDB

Reputation: 25810

Typically you use try...catch statements for those statements which you expect to throw an Exception. Try...Catch defines its own scope, so you should declare any variables outside of the Try...Catch block (at least, those variables that you want to use outside of it).

If you want to know if an exception was thrown, then define the Exception variable above the Try...Catch. You can then examine it to determine if it is Null or not.

System.Exception ex;
try {
  Client.connect();
} catch (ex) {
  // Do some exception handling...
} finally {
  // Do some cleanup...
}

if (ex != null){ ... }

// Talk to the server...  

You could log an event and then call some code to either try again or to cancel... or whatever you need to do.

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

The easiest way is to just set a boolean. But there are many many many ways to deal with this.

bool connectionError = false;

try {
    // connect
} catch (...) {
    connectionError = true;
} finally {
   // whatever
}

if (!connectionError) {
    // talk to server.
}

Upvotes: 2

Eric J.
Eric J.

Reputation: 150108

"Talk to the server" should happen in the try block, right after

Client.connect();

Upvotes: 13

Related Questions