473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Need "Try" clause to catch database connection error

I setup a "Try..." block and attempted to open a non-
existant database. It went to the second "catch" not the
first. What is the proper "catch" clause for this specific
case?

TIA,

Bill
try
conn.open ' will generate an error
....

Catch ex As InvalidOperationException
...
Catch ex As Exception

end try
Nov 20 '05 #1
6 4425
SqlException, I think. From MSDN:
Exception Type Condition
InvalidOperationException Cannot open a connection without specifying
a data source or server.
or

The connection is already open.

SqlException A connection-level error occurred while opening the
connection.
"bill salkin" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I setup a "Try..." block and attempted to open a non-
existant database. It went to the second "catch" not the
first. What is the proper "catch" clause for this specific
case?

TIA,

Bill
try
conn.open ' will generate an error
...

Catch ex As InvalidOperationException
..
Catch ex As Exception

end try

Nov 20 '05 #2
"bill salkin" <an*******@discussions.microsoft.com> schrieb
I setup a "Try..." block and attempted to open a non-
existant database. It went to the second "catch" not the
first. What is the proper "catch" clause for this specific
case?

TIA,

Bill
try
conn.open ' will generate an error
...

Catch ex As InvalidOperationException
..
Catch ex As Exception

end try

Why not have a look at the caught exception?

The docs for the open method say that an OleDBException is thrown.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #3
Bill,
It depends on the type of object conn is!

For example according to MSDN OdbcConnection.Open will throw either an
InvalidOperationException or an OdbcException. While SqlConnection.Open will
throw either an InvalidOperationException or an SqlException.

What I normally do is debug it during development then check what type of
exception "Catch ex As Exception" actually caught. I then modify my program
to catch that specific type.

Hope this helps
Jay
"bill salkin" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I setup a "Try..." block and attempted to open a non-
existant database. It went to the second "catch" not the
first. What is the proper "catch" clause for this specific
case?

TIA,

Bill
try
conn.open ' will generate an error
...

Catch ex As InvalidOperationException
..
Catch ex As Exception

end try

Nov 20 '05 #4
* "bill salkin" <an*******@discussions.microsoft.com> scripsit:
I setup a "Try..." block and attempted to open a non-
existant database. It went to the second "catch" not the
first. What is the proper "catch" clause for this specific
case? [...] try
conn.open ' will generate an error
...

Catch ex As InvalidOperationException
..
Catch ex As Exception

end try


Have a look at the documentation for the connection's 'Open' method you
use ('OleDbConnection', 'SqlConnection', OdbcConnection, ...).

The docs say:

'OleDbConnection'

'InvalidOperationException' if connection already open,
'OleDbException' for errors in the connection level.

'SqlConnection'

See: <http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdatasqlclientsqlconnectionclassopentopi c.asp>

'OdbcConnection'

See: <http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemdataodbcodbcconnectionclassopentopic.as p>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
Jay,

You wrote

"What I normally do is debug it during development then
check what type of exception "Catch ex As Exception"
actually caught."

HOw do you check the exception type?

TIA,

Bill
-----Original Message-----
Bill,
It depends on the type of object conn is!

For example according to MSDN OdbcConnection.Open will throw either anInvalidOperationException or an OdbcException. While SqlConnection.Open willthrow either an InvalidOperationException or an SqlException.
What I normally do is debug it during development then check what type ofexception "Catch ex As Exception" actually caught. I then modify my programto catch that specific type.

Hope this helps
Jay
"bill salkin" <an*******@discussions.microsoft.com> wrote in messagenews:01****************************@phx.gbl...
I setup a "Try..." block and attempted to open a non-
existant database. It went to the second "catch" not the
first. What is the proper "catch" clause for this specific case?

TIA,

Bill
try
conn.open ' will generate an error
...

Catch ex As InvalidOperationException
..
Catch ex As Exception

end try

.

Nov 20 '05 #6
Bill,
HOw do you check the exception type? Any one of the watch windows (Autos, Locals, Watch 1 thru 4).

The type column will give you the actual type of the ex object, not just the
type of the variable.

Hope this helps
Jay
"bill salkin" <an*******@discussions.microsoft.com> wrote in message
news:04****************************@phx.gbl... Jay,

You wrote

"What I normally do is debug it during development then
check what type of exception "Catch ex As Exception"
actually caught."

HOw do you check the exception type?

TIA,

Bill
-----Original Message-----
Bill,
It depends on the type of object conn is!

For example according to MSDN OdbcConnection.Open will

throw either an
InvalidOperationException or an OdbcException. While

SqlConnection.Open will
throw either an InvalidOperationException or an

SqlException.

What I normally do is debug it during development then

check what type of
exception "Catch ex As Exception" actually caught. I then

modify my program
to catch that specific type.

Hope this helps
Jay
"bill salkin" <an*******@discussions.microsoft.com> wrote

in message
news:01****************************@phx.gbl...
I setup a "Try..." block and attempted to open a non-
existant database. It went to the second "catch" not the
first. What is the proper "catch" clause for this specific case?

TIA,

Bill
try
conn.open ' will generate an error
...

Catch ex As InvalidOperationException
..
Catch ex As Exception

end try

.

Nov 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jeff User | last post by:
Hi all I am using an OleDbDataReader. I need to establish and then keep the connection that I use, but I do not need to keep the data reader, after this operation is over. Therefore, regardless...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
20
by: DraguVaso | last post by:
Hi, As a former VB-programmer I'm used to the "on error goto"-errorhandling. I see that in actually all the VB.NET-samples I can fin people use the "Try - catch"-errorhandling. Which of the...
7
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
10
by: pauldepstein | last post by:
I have read about exception handling in a few books and websites but am still confused on a basic point. I understand the try ... catch syntax. However, I have seen examples of using throw...
5
by: Morten Snedker | last post by:
The use of Try in the Finally part - is that overkill?. I think of the code failing before opening sqlCon - that would generate an error in the Finally part. How would Finally handle that? Try...
16
by: HillBilly | last post by:
This is freaking me out. I'm using Membership and trying to determine if the database is online. The GetConnectionString( ) method returns a connection string as expected but not when used in the...
6
by: rhaazy | last post by:
I am looking for some feedback on using try catch statements. Usually when I start a project I use them for everything, but stop using them as often after the "meat n' potatos" of the project is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.