Error Handling Instructions
From Kietzman.org
Contents |
Introduction
Error handling should be used in all aspects of coding. This will help keep your applications from hanging, core dumping, terminating unexpectedly or executing when the program should be terminating. Error handling should be implemented for both base and web code.
There are three types of error handling.
- function return codes
- try/catch exception handling
- signal handling
Function Return Codes
Functions of any programming language are capable of returning a result. One of the easiest methods for testing whether a function was successfull is by utilizing its return value with a status/error code. Most times you will use a basic boolean value of true/false as a return value. If the objective of the function was to return data, you should always pass the data as a function parameter and not as a return value.
Example
#include <iostream>
#include <string>
using namespace std;
bool getContent(string strSearch, string strDirectory);
int main(int argc, char *argv[])
{
string strSearch = "test";
string strDirectory = "./";
if (getContent(strSearch, strDirectory))
{
cout << "function passed" << endl;
}
else
{
cout << "function failed" << endl;
}
return 0;
}
bool getContent(string strSearch, string strDirectory)
{
bool bResult = false;
string strCommand = (string)"grep " + strSearch + (string)" " + strDirectory;
if (system(strCommand.c_str()) == 0)
{
// System command completed sucessfully
bResult = true;
}
return bResult;
}
Try/Catch Exception Handling
In many programming languages, like C++, Java and PHP, there is the ability to throw an error when encountering a problem. In order to catch a thrown error you must strap a try statement around the logic that throws the error. Then a catch statement is used to let the program know what operations to perform when that certain type of error is caught. This is different than signal handling since the exceptions are usually generated by the application itself rather than the operating system. This will help in keeping your applications from hanging, core dumping, terminating unexpectedly or executing when the program should be terminating.
Example
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
try
{
// Try and grab any available memory to use in your application
char *pszBuffer = new char[1024];
if (!pszBuffer)
{
throw("Could not allocate memory.");
}
}
catch (char *pszCatch)
{
cout << pszCatch << endl;
}
return 0;
}
Signal Handling
Most compiled languages, like C/C++, provide the ability to recognize signals in the operating system. If a program is designed well, it should be able to respond to system signals appropriately. This is different than try/catch exception handling since the signals are usually coming from the operating system or the user themselves and not being generated by the application itself.
Common Signals
| Signal | Number | Meaning | Description |
|---|---|---|---|
| sighup | 1 | hangup | request for application restart |
| sigint | 2 | interrupt | user request for break |
| sigabrt | 6 | abort | used by abort function |
| sigfpe | 8 | floating point | floating point exception |
| sigkill | 9 | kill | cannot be caught or ignored |
| sigbus | 10 | bus error | file IO exception |
| sigsegv | 11 | segmentation fault | memory allocation exception |
| sigpipe | 13 | pipe | write on pipe with no one to read |
| sigterm | 15 | terminate | request for application shutdown |
| sigchld | 18 | child | child status change |
| sigwinch | 20 | window change | window size change |
| sigstop | 23 | stop | cannot be caught or ignored |
Example
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
#include <general/SignalHandling>
using namespace general;
void sighandle(const int nSignal);
int main(int argc, char *argv[])
{
sethandles(sighandle);
sigignore(SIGPIPE);
return 0;
}
void sighandle(const int nSignal)
{
string strTemp;
stringstream outStream;
if (nSignal == SIGCHLD)
{
wait3(NULL, WNOHANG, NULL);
}
else if (nSignal == SIGINT)
{
sethandles(sigdummy);
outStream << "User interrupt request. Exiting...";
exit(0);
}
else if (nSignal == SIGTERM)
{
sethandles(sigdummy);
outStream << "Operating system is asking for termination. Exiting...";
exit(0);
}
else
{
sethandles(sigdummy);
outStream << "The program's signal handling caught a "
<< sigstring(strTemp, nSignal)
<< "(" << nSignal << ")! Exiting...";
exit(1);
}
}
