Re: Evaluating Exceptions, Try Except and Try Finally

From: Skybuck Flying (nospam_at_hotmail.com)
Date: 04/08/04


Date: Thu, 8 Apr 2004 19:50:48 +0200


"Skybuck Flying" <nospam@hotmail.com> wrote in message
news:c53vhg$pvq$1@news5.tilbu1.nb.home.nl...
>
> "Rob Kennedy" <me3@privacy.net> wrote in message
> news:c53ssh$2pd76l$1@ID-220940.news.uni-berlin.de...
> > Skybuck Flying wrote:
> > > Try
> > > Try
> > > Connection.Open
> > > Finally
> > > Connection.Close
> > > end;
> > > Except
> > > On Exception
> > > end;
> > >
> > > The only big drawback is that... the exception will happen after the
> > > connection is closed.. that's weird !
> >
> > If there was an exception, then the connection was never opened in the
> > first place
>
> No, that's where you are wrong because after the open statement other
> instructions
> follow which can also procedure exceptions.
>
> That's why the try except block is surrounding the try finally block, so
to
> catch those exceptions.

Though I now realize that this pseudo code would be better.

The documentation in delphi about try/finally was hard to understand for
me... see other posts =D

The documentation read: "finally will always get executed"! That is
ofcourse not true if an exception happens before the try block is entered !

Actually I saw some other programming language I think it was visual
basic...and I thought nicccee:

   try

   catch

    finally

    end;

Or something like:

try
{

}
finally
{

}

Delphi just has:

try

nothing <- a bit weird.

finally

end;

Oh well.

I now see how this pseudo code would be better:

try
    Connection.Open;

    IF AN EXCEPTION OCCURS HERE THE TRY FINALLY BLOCK WILL BE SKIPPED !
    SO CLOSE WILL NEVER GET CALLED WHICH IS PROBABLY OK ANYWAY
    SINCE THE CONNECTION DID NOT OPEN ???!!!??
    UNLESS OFCOURSE IT DID OPEN BUT SOME OTHER KINDS OF WACKY EXCEPTION
OCCURED !!!!

    HAHAHAHAHA

    THAT ONCE AGAIN PROOFS HOW STUPID EXCEPTIONS ARE !!!!!!!!

    WHAT IF A STUPID DIV BY ZERO OCCURED OR SO WHICH IS NON CRITICAL AND THE
CONNECTION
    WAS STILL OPENED !!!!

    THERE IS JUST NO WAY TO KNOW FOR SURE...

    UNCERTAINTY/DOUBTFULL/AMBIGUITY :):):)

    UNLESS SOME STUPID DOCUMENTATION SAYS:

    "NONONONONONO CONNECTION.OPEN WILL NEVER RAISE AN EXCEPTION IF IT DID
OPEN"

    I'll bet a billion bucks the .NET doc doesn't say that !

    So in short:

    What if Connection.Open did succeed... but some kind of other exception
was raised. I don't care what it was...

    The result is: The connection won't be closed ! so we still have a god
damn problem !

    HA HA HA HA HA

    All this shit could be easily solved with IF STATEMENTS !!!

    if Connection.Open then <---- CERTAINTY ! :P
    begin

        Connection.Close;
    end;

    There is no way this logic would ever fail ! :P

Rest of stupid exception code:

    try

        Execute SQL

    finally

        Connection.Close; // always close connection if it opened. unless
open did work but other stupid exception was raised.

    end;

except

    // catch connection exception

    or

    // catch execute sql exception

    // catch other exceptions !

end;

.NET documentation:

Opens a database connection with the settings specified by the
ConnectionString property of the provider-specific Connection object.

[Visual Basic]
Sub Open()
[C#]
void Open();
[C++]
void Open();
[JScript]
function Open();
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows
2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003
family, .NET Compact Framework - Windows CE .NET

See Also
IDbConnection Interface | IDbConnection Members | System.Data Namespace |
IDbConnection Members (Visual J# Syntax) | Managed Extensions for C++
Programming

Syntax based on .NET Framework version 1.1.
Documentation version 1.1.0.

----------------------------------------------------------------------------

----
It does even say that it could throw exceptions !
Let alone what other exceptions it could raise !
Skybuck.


Relevant Pages

  • Re: Programming - Best Practice
    ... By using a dataadapter you don't even have to open and close a connection as ... it opens and closed it and leaves it in the state it was as it is closed. ... object of those types that is not open will NOT cause an exception. ... The conditional execution ...
    (microsoft.public.dotnet.languages.vb)
  • RE: System.InvalidOperationException: There is already an open DataReader associated with this Conne
    ... you already had a DataReader open on the connection before Update method is ... then the exception is thrown. ... opens a DataReader on the same connection that the DataAdapter uses. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Oracle Connection Problem
    ... > The exception occurs on the Open method of the connection and the error ... object and did so without exception. ... It is only when the connection object ... If you have a piece of code which opens connections, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Connection: how to survive server restart?
    ... the pooler opens it for you and ... holds the connection open after it's closed so if the server goes down you ... and returns the exception to your code. ... the next open will succeed (assuming the server is ...
    (microsoft.public.dotnet.framework.adonet)
  • RE: media center error?????
    ... Try modifying your "windows firewall" settings either disable it altogether ... or make an exception for windows media center... ... disabling it completely at least temporarily and then if that works fine tune ... the connections tab and disable it for your individual network connection ...
    (microsoft.public.windows.mediacenter)