Results 1 to 8 of 8

Thread: try... except

  1. #1

    try... except

    Hi guys.

    Is there a way to do this?:

    ////////////////////////////////////////

    try

    if MessageDlg('Press Yes or No', mtConfirmation, [MBYES, MBNO], 0) = MRYES then GOtoEXCEPTend;

    except
    ShowMessage('The user clicked on YES button.');
    end;

    ////////////////////////////////////////

    Thank you.

  2. #2

    try... except

    You can leave out the Try... Except there ... since the MessageDlg is unlikely to fail

    [pascal]
    if MessageDlg('Press Yes or No', mtConfirmation, [MBYES, MBNO], 0) = MRYES then
    Begin
    ShowMessage('The user clicked on YES button.');
    End
    Else
    Begin
    ShowMessage('The user clicked the NO button.');
    End;
    [/pascal]


    It might be that I misunderstood what you where trying to do, however I don't see the need for try...except in this scenario... If you want to jump from the loop if the user presses the yes / no button you could use the exit command...
    Do it by the book, but be the author!
    <br />
    <br />Visit the Lion Productions website at:
    <br />http://lionprod.f2o.org

  3. #3
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    try... except

    Yes there is a way to do it but dont - it is really bad coding.

    As TheLion Suggests - there are better ways.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  4. #4

    try... except

    Yes!

    But I have other things in mind: Look, let's say I have this code:


    try

    ...

    GetMem(p, 10000);
    Object.SomeProcedure();
    OtherObject.Procedure(10, 10);

    open:= OpenSomething();

    if open = FALSE then GoToExceptEnd;

    ...

    except
    FreeMem(p);
    Object.Free;
    OtherObject.Free;
    CloseSomething();
    end;


    In this example, I have made calls to 4 procedures/functions: GetMem, SomeProcedure, Procedure and OpenSomething.

    If something goes wrong with one of them, I have to free all the resources used by the code. I do that in the except/end statement.

    But the problem is that the fourth function (OpenSomething) don't raise exceptions. Instead of this, it returns FALSE or TRUE. If it returns FALSE, I would like the code jump to the Except End to, as if an exception were raised.

    Is there a way to do that?

    Thank you!

  5. #5

    try... except

    Well, if you really want to, you could try putting it either in a try..finally block(but only if you need to free those things in any case, which I doubt). OR you could put the freeing in a seperate procedure, and do it like this:

    try

    ...

    GetMem(p, 10000);
    Object.SomeProcedure();
    OtherObject.Procedure(10, 10);

    open:= OpenSomething();

    if open = FALSE then FreeProcedure;
    ...

    except
    FreeProcedure;
    end;




    FreeProcedure;
    begin
    FreeMem(p);
    Object.Free;
    OtherObject.Free;
    CloseSomething();
    end;




    But there might be other ways....
    -Sander

  6. #6

    try... except

    try
    AllocateSomeMemory(X);
    except
    ShowAWarning;
    finally
    if Assigned(X) then
    FreeMemory(X);
    end;
    Ask me about the xcess game development kit

  7. #7

    try... except

    How about raising an exception yourself if the function returns false?

    [pascal]try
    ...
    if open = FALSE then raise Exception.Create('open failed');

    ...

    except
    FreeMem(p);
    Object.Free;
    OtherObject.Free;
    CloseSomething();
    end;[/pascal]
    [size=10px][ Join us in #pgd on irc.freenode.net ] [ Sign the Petition for a Software Patent Free Europe ][/size]

  8. #8

    try... except

    My god! A lot of solutions! :-)

    THank you guys! I enjoyed all the examples!
    Fernando

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •