Error Trapping Application Class (Example)

See below the example, which uses application package & class, to trap the error comes in your program. This is reusable in all programs so I found it very useful.

/* Code to write in your code to call Error trap method */

import PKG:CLASS_PKG:*;

Local CLASSname &objName = create CLASSname();

try
—–
catch Exception &ex1
&objName.errorHandler(&ex1, “EOTP_PKG.EOTP_MAIN_PKG.TPMUtil.log”, True);
end-try;


/* Code to write in App Class for Error trap */

method errorHandler(&inputException As Exception, &logToFileName As string, &writeToFile As boolean);

/** This method writes the collection of error messages to a log file.
* Automatically puts the document in the directory specified by the
* TEMP system variable.
*
* So, on Windows, if %TEMP is defined to be C:\temp, then it will put
* the file into C:\temp\<&logToFileName> .
*
* If the &writeToFile parameter is true, then the method will write the
* output to a file. Otherwise, if you just want the output to be written
* to the screen in a MessageBox, you can leave the &logToFileName parameter
* blank (make it “” or something — the method won’t pay attention to it),
* and make the &writeToFile parameter False.
*/

method errorHandler
/+ &inputException as Exception, +/
/+ &logToFileName as String, +/
/+ &writeToFile as Boolean +/
Local number &i;
Local string &strErrMsgSetNum, &strErrMsgNum, &strErrMsgText, &strErrType;

&strErrMsgSetNum = String(&inputException.MessageSetNumber);
&strErrMsgNum = String(&inputException.MessageNumber);
&strErrMsgText = String(&inputException.ToString());
If (&writeToFile) Then
Local File &logToFile = GetFile(GetEnv(”TEMP”) | “\” | &logToFileName, “a”, %FilePath_Absolute);
&logToFile.WriteLine(%Datetime | “:”);
&logToFile.WriteLine(&strErrType | ” (” | &strErrMsgSetNum | “,” | &strErrMsgNum | “) - ” | &strErrMsgText);
Else
MessageBox(0, “”, &inputException.MessageSetNumber, &inputException.MessageNumber, &inputException.ToString());
End-If;
end-method;

admin posted at 2009-1-15 Category: PeopleCode Tips | Tags: , ,

Leave a Reply

(Ctrl + Enter)