Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

General Try-Catch question

Status
Not open for further replies.

sparafucile17

Programmer
Apr 5, 2002
40
US
Ok, here's the deal: I want to try to open a database but want to know if the connection was successful or not. But the only way I know that it wasn't successful is that a sql exception is fired. So I was thinking that I could use a bool to keep track of connection status like so:


boolean isConnected;
try
{
DriverManager.getConnection("jdbc:codebase:192.168.0.1:1583/CodebaseODBC", "User", "Pass");
isConnected = true;
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Cannot connect!");
isConnected = false;
}


My worry here is that the isConnected=true; statement will be executed regardless of exceptions. Is this hunch correct? I'm a little clueless when it comes to the try-catch command.

If this doesn't work, does anyone have any other ideas?

Regards,
Jeff
 
When a statement causes an exception, the try block is exited and the catch block is executed. In this case, if the getConnection throws a SQLException, isConnected=true would not be executed. "When you have eliminated the impossible, whatever remains, however
improbable, must be the truth." ~ Arthur Conan Doyle
 
>> Is this hunch correct?

No, idarke is correct.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top