Wednesday, March 18, 2020

On Handling Exceptions in Delphi Exception Handling

On Handling Exceptions in Delphi Exception Handling Heres an interesting fact: No code is error free - in fact, some code is full of errors on purpose. Whats an error in an application? An error is an incorrectly coded solution to a problem. Such are logic errors that could lead to wrong function results where everything seems nicely put together but the result of the application is completely unusable. With logic errors, an  application might or might not stop working. Exceptions can include errors in your code where you try to divide numbers with zero, or you try using freed memory blocks  or try providing wrong parameters to a function. However, an exception in an application is not always an error. Exceptions and the Exception Class Exceptions are special conditions that require special handling. When an error-type condition occurs the program raises an exception. You (as the application writer) will handle exceptions to make your application more error-prone and to respond to the exceptional condition. In most cases, you will find yourself being the application writer and also the library writer. So you would need to know how to raise exceptions (from your library) and how to handle them (from your application). The article on handling errors and exceptions provides some basic guidelines on how to guard against errors using try/except/end and try/finally/end protected blocks to respond to or handle exceptional conditions. A simple try/except guarding blocks looks like: try ThisFunctionMightRaiseAnException();except//handle any exceptions raised in ThisFunctionMightRaiseAnException() hereend; The ThisFunctionMightRaiseAnException might have, in its implementation, a line of code like raise Exception.Create(special condition!); The Exception is a special class (one of a few without a T in front of the name) defined in sysutils.pas unit. The SysUtils unit defines several special purpose Exception descendants (and thus creates a hierarchy of exception classes) like ERangeError, EDivByZero, EIntOverflow, etc. In most cases, the exceptions that you would handle in the protected try/except block would not be of the Exception (base) class but of some special Exception descendant class defined in either the VCL or in the library you are using. Handling Exceptions Using Try/Except To catch and handle an exception type you would construct a on type_of_exception do exception handler. The on exception do looks pretty much like the classic case statement: try ThisFunctionMightRaiseAnException;excepton EZeroDivide dobegin//something when dividing by zeroend; on EIntOverflow dobegin//something when too large integer calculationend; elsebegin//something when other exception types are raisedend;end; Note that the else part would grab all (other) exceptions, including those you know nothing about. In general, your code should handle only exceptions you actually know how to handle and expect to be thrown. Also, you should never eat an exception: try ThisFunctionMightRaiseAnException;exceptend; Eating the exception means you dont know how to handle the exception or you dont want users to see the exception or anything in between. When you handle the exception and you need more data from it (after all it is an instance of a class) rather only the type of the exception you can do: try ThisFunctionMightRaiseAnException;excepton E : Exception dobegin ShowMessage(E.Message); end;end; The E in E:Exception is a temporary exception variable of type specified after the column character (in the above example the base Exception class). Using E you can read (or write) values to the exception object, like get or set the Message property. Who Frees The Exception? Have you noticed how exceptions are actually instances of a class descending from Exception? The raise keyword throws an exception class instance. What you create (the exception instance is an object), you also need to free. If you (as a library writer) create an instance, will the application user free it? Heres the Delphi magic: Handling an exception automatically destroys the exception object. This means that when you write the code in the except/end block, it will release the exception memory. So what happens if ThisFunctionMightRaiseAnException actually raises an exception and you are not handling it (this is not the same as eating it)? What About When Number/0 Is Not Handled? When an unhandled exception is thrown in your code, Delphi again magically handles your exception by displaying the error dialog to the user. In most cases, this dialog will not provide enough data for the user (and finally you) to understand the cause of the exception. This is controlled by Delphis top level message loop where all exceptions are being processed by the global Application object and its HandleException method. To handle exceptions globally, and show your own more-user-friendly dialog, you can write code for the TApplicationEvents.OnException event handler. Note that the global Application object is defined in the Forms unit. The TApplicationEvents is a component you can use to intercept the events of the global Application object.

Sunday, March 1, 2020

The Code of Justinian (Codex Justinianus)

The Code of Justinian (Codex Justinianus) The Code of Justinian (in Latin, Codex Justinianus) is a substantial collection of laws compiled under the sponsorship of Justinian I, ruler of the Byzantine Empire. Although the laws passed during Justinians reign would be included, the Codex was not a completely new legal code, but an aggregation of existing laws, portions of the historic opinions of great Roman legal experts, and an outline of law in general. Work began on the Code shortly after Justinian took the throne in 527. While much of it was completed by the mid-530s, because the Code included new laws, parts of it were regularly revised to include those new laws, up until 565. There were four books that comprised the Code: Codex Constitutionum, the Digesta, the Institutiones and the Novellae Constitutiones Post Codicem. The Codex Constitutionum The Codex Constitutionum was the first book to be compiled. In the first few months of Justinians reign, he appointed a commission of ten jurists to review all the laws, rulings and decrees issued by the emperors. They reconciled contradictions, weeded out obsolete laws, and adapted archaic laws to their contemporary circumstances. In 529 the results of their efforts were published in 10 volumes and disseminated throughout the empire. All imperial laws not contained in the Codex Constitutionum were repealed. In 534 a revised codex was issued that incorporated the legislation Justinian had passed in the first seven years of his reign. This Codex Repetitae Praelectionis was comprised of 12 volumes. The  Digesta The Digesta (also known as the Pandectae) was begun in 530 under the direction of Tribonian, an esteemed jurist appointed by the emperor. Tribonian created a commission of 16 attorneys who combed through the writings of every recognized legal expert in imperial history. They culled whatever they though was of legal value and selected one extract (and occasionally two) on each legal point. They then combined them into an immense collection of 50 volumes, subdivided into segments according to subject. The resulting work was published in 533. Any juridical statement that wasnt included in the Digesta was not considered binding, and in future it would no longer be a valid basis for legal citation. The  Institutiones When Tribonian (along with his commission) had finished the Digesta, he turned his attention to the Institutiones. Pulled together and published in about a year, the Institutiones was a basic textbook for beginning law students. It was based on earlier texts, including some by the great Roman jurist Gaius, and provided a general outline of legal institutions. The  Novellae Constitutiones Post Codicem After the revised Codex was published in 534, the last publication, the Novellae Constitutiones Post Codicem was issued. Known simply as the Novels in English, this publication was a collection of the new laws the emperor had issued himself. It was reissued regularly until Justinians death. With the exception of the Novels, which were almost all written in Greek, the Code of Justinian was published in Latin. The Novels also had Latin translations for the western provinces of the empire. The Code of Justinian would be highly influential through much of the Middle Ages, not only with the Emperors of Eastern Rome, but with the rest of Europe.   Resources and Further Reading Grapel, William.  The Institutes of Justinian: with the Novel as to Successions. Lawbook Exchange, Ltd., 2010.Mears, T. Lambert, et al.  Analysis of M. Ortolans Institutes of Justinian, Including the History and Generalization of Roman Law. Lawbook Exchange, 2008.