问题
Ok, I am currently a Grade 11 student thats taking IT. I am trying to finish a Practical assignment but I ran into a bit of a problem, the textbook I am using didn't show me how to Create an error message if the user did not enter data into a RichEdit. Could anyone advise me on how to do this? thank you for taking the time to help.
回答1:
This is how you raise a generic exception (using the SysUtils.Exception
class):
raise Exception.Create('Error Message');
An unhandled exception causes the execution path to escape into a default exception handler inside of the Delphi RTL, which will then display the value of the Exception.Message
to the user.
You could even handle your own exception like this:
try
...
raise Exception.Create('Error Message');
...
except
on E: Exception do
begin
ShowMessage(E.Message);
end;
end;
You wouldn't actually do this though. You raise exceptions so that code calling your method can handle the error.
Raise an exception if you want to handle the error elsewhere (in the caller).
To simply display the system standard error dialog, you can use MessageDlg
:
MessageDlg('Error Message', mtError, [mbOK], 0);
The caption of the window in this case is simply "Error". If you must set a caption, use CreateMessageDialog
:
with CreateMessageDialog('Error Message', mtError, [mbOK], mbOK) do
begin
try
Caption := 'Error Caption';
ShowModal;
finally
Release;
end;
end;
The Exception
class is in System.SysUtils
. MessageDlg
and CreateMessageDialog
are in Vcl.Dialogs
.
Or use the TApplication.MessageBox()
method:
Application.MessageBox('Error Message', 'Error Caption', MB_OK or MB_ICONERROR);
来源:https://stackoverflow.com/questions/19252589/create-error-messages-delphi-7