Connecting Tech Pros Worldwide Forums | Help | Site Map

Need "Try" clause to catch database connection error

bill salkin
Guest
 
Posts: n/a
#1: Nov 20 '05
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



Mike Hildner
Guest
 
Posts: n/a
#2: Nov 20 '05

re: Need "Try" clause to catch database connection error


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" <anonymous@discussions.microsoft.com> wrote in message
news:01bd01c3cf1d$3b623940$a601280a@phx.gbl...[color=blue]
> 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
>
>[/color]


Armin Zingler
Guest
 
Posts: n/a
#3: Nov 20 '05

re: Need "Try" clause to catch database connection error


"bill salkin" <anonymous@discussions.microsoft.com> schrieb[color=blue]
> 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[/color]


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

Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#4: Nov 20 '05

re: Need "Try" clause to catch database connection error


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" <anonymous@discussions.microsoft.com> wrote in message
news:01bd01c3cf1d$3b623940$a601280a@phx.gbl...[color=blue]
> 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
>
>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#5: Nov 20 '05

re: Need "Try" clause to catch database connection error


* "bill salkin" <anonymous@discussions.microsoft.com> scripsit:[color=blue]
> 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?[/color]
[...][color=blue]
> try
> conn.open ' will generate an error
> ...
>
> Catch ex As InvalidOperationException
> ..
> Catch ex As Exception
>
> end try[/color]

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>
bill salkin
Guest
 
Posts: n/a
#6: Nov 20 '05

re: Need "Try" clause to catch database connection error


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


[color=blue]
>-----Original Message-----
>Bill,
>It depends on the type of object conn is!
>
>For example according to MSDN OdbcConnection.Open will[/color]
throw either an[color=blue]
>InvalidOperationException or an OdbcException. While[/color]
SqlConnection.Open will[color=blue]
>throw either an InvalidOperationException or an[/color]
SqlException.[color=blue]
>
>What I normally do is debug it during development then[/color]
check what type of[color=blue]
>exception "Catch ex As Exception" actually caught. I then[/color]
modify my program[color=blue]
>to catch that specific type.
>
>Hope this helps
>Jay
>
>
>"bill salkin" <anonymous@discussions.microsoft.com> wrote[/color]
in message[color=blue]
>news:01bd01c3cf1d$3b623940$a601280a@phx.gbl...[color=green]
>> 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[/color][/color]
specific[color=blue][color=green]
>> case?
>>
>> TIA,
>>
>> Bill
>>
>>
>> try
>> conn.open ' will generate an error
>> ...
>>
>> Catch ex As InvalidOperationException
>> ..
>> Catch ex As Exception
>>
>> end try
>>
>>[/color]
>
>
>.
>[/color]
Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#7: Nov 20 '05

re: Need "Try" clause to catch database connection error


Bill,[color=blue]
> HOw do you check the exception type?[/color]
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" <anonymous@discussions.microsoft.com> wrote in message
news:042901c3cf42$c672a860$a501280a@phx.gbl...[color=blue]
> 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
>
>
>[color=green]
> >-----Original Message-----
> >Bill,
> >It depends on the type of object conn is!
> >
> >For example according to MSDN OdbcConnection.Open will[/color]
> throw either an[color=green]
> >InvalidOperationException or an OdbcException. While[/color]
> SqlConnection.Open will[color=green]
> >throw either an InvalidOperationException or an[/color]
> SqlException.[color=green]
> >
> >What I normally do is debug it during development then[/color]
> check what type of[color=green]
> >exception "Catch ex As Exception" actually caught. I then[/color]
> modify my program[color=green]
> >to catch that specific type.
> >
> >Hope this helps
> >Jay
> >
> >
> >"bill salkin" <anonymous@discussions.microsoft.com> wrote[/color]
> in message[color=green]
> >news:01bd01c3cf1d$3b623940$a601280a@phx.gbl...[color=darkred]
> >> 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[/color][/color]
> specific[color=green][color=darkred]
> >> case?
> >>
> >> TIA,
> >>
> >> Bill
> >>
> >>
> >> try
> >> conn.open ' will generate an error
> >> ...
> >>
> >> Catch ex As InvalidOperationException
> >> ..
> >> Catch ex As Exception
> >>
> >> end try
> >>
> >>[/color]
> >
> >
> >.
> >[/color][/color]


Closed Thread