sloppycode.net
Exception handling in Delphi
An example of errors and handling them.
Home
›
Code snippets
›
Delphi
›
Exception handling in Delphi
Below is some examples of how to implement error handling in Delphi. It's similar to Java (or Java is similiar to Delphi ) with it's try catch blocks. Detailed info on the Exception object is in the Delphi help file, along with a list of standard VCL E* exception objects.
{ ----- Simple Try/Except block ----- } try // try something here except // Exception, throw an error message end; { ----- Try/Except/Finally block ----- } try // Try something except // Exception, throw an error message finally // Do clean up code end; { ----- Catching specific errors (prefered method) ----- } try // Try something except on EInvalidArgument do begin // Handle an EInvalidArgument exception here end; on EInvalidPointer do begin // Handle an EInvalidPointer exception here etc. end; finally // Optional final clause end; { ----- Raising an exception ----- } // You can raise any object instance for an error. if not i=1 then begin raise EInvalidArgument.Create('Tut tut bad parameters used'); end; { ----- Creating your own exception ----- } // This will derive the class from the Exception object // which is the best way to do it. This means if you error // object doesn't handle the error, the Exception object will. // The exception object has the following methods: // - Create - takes a string message as its parameter // - CreateFmt - Variation on the create constructor, different args // - CreateFmtHelp - Same as above // - CreateHelp - All the ones below + this one are the same // - CreateRes // - CreateResFmt // - CreateResFmtHelp // - CreateResHelp type EMyException = class(Exception);
{Name}
Says:
{Date}
{Text}
› Home
› C#
› Snippets
› Articles
› Tools
› Taglines
› ASP
› Dictionary Object
› FSO
› Unix cheat sheet
› Gaming
› CSS
› Yak
› Umbraco
› About
› Contact
› Privacy
› Projects
› Search
› Sitemap
Buy on Amazon
Buy on Amazon
Buy on Amazon
Buy on Amazon