One big thing you need to know how to use is the TRY block. There's two ways you can use it...
Code:
Q:= TADOQuery.Create(nil);
try
Q.Connection:= MyConnection;
//Do some stuff with Q
finally
if assigned(Q) then Q.Free;
end;
The try..finally block is intended for safe-guarding code and making sure that if anything fails in that block of code, that the code in the finally section will still be executed at the end. Any exception which is raised between TRY and FINALLY will jump down to the code between FINALLY and END.
or...
Code:
try
//Safe code goes here
except
//Exception handling goes here
on e: exception do begin
ShowMessage('An error has occured: '+e.Message);
end;
end;
The try..except block is intended for safe-guarding code and catching if an error occurs. If any exception is raised between TRY and EXCEPT, it will jump down to the exception handler(s) between EXCEPT and END. Catch each exception type as shown above (99% of the time you would use "on e: exception do..." and e being anything you want to name it, like ERR or Except.
Some errors are hard to figure out. For example, when working with sockets (in ScktComp unit of Delphi), if an error is raised, even if you have an event handler for the error, it still raises its own internal error and displays a message regardless of any exception handler. In this case, they provide an extra ability: In the error event handler, there's a var parameter "ErrorCode". If I switch that integer to "0" in the event handler, the socket does not raise its internal error. If I ignore the ErrorCode parameter, it will raise the internal exception.
So it kinda depends on how you're doing it. If you're doing FreeAndNil, then FreeAndNil would need to go between FINALLY and END, while the initialization which required you to free will need to go just before the TRY. Any code using this variable must be safe-coded between TRY and FINALLY. Just like I declared the Q: TADOQuery above. In the FINALLY section, all I do is free that Q I created.
JD Solutions