added some exceptions to handle errors

develop
mizipzor 2010-02-28 01:13:34 +01:00
parent 562c30c703
commit 4f85f37b01
4 changed files with 81 additions and 10 deletions

@ -9,23 +9,35 @@
using namespace std;
#include <DFTypes.h>
#include <DFHackAPI.h>
#include <DFError.h>
int main (void)
{
time_t start, end;
double time_diff;
DFHack::API DF("Memory.xml");
if(!DF.Attach())
//if(!DF.Attach())
//{
// cerr << "DF not found" << endl;
// return 1;
//}
//if(!DF.Detach())
//{
// cerr << "Can't detach from DF" << endl;
// return 1;
//}
try
{
cerr << "DF not found" << endl;
return 1;
DF.Attach();
DF.Detach();
}
if(!DF.Detach())
catch (DFHack::Error::NoProcess& e)
{
cerr << "Can't detach from DF" << endl;
cerr << e.what() << endl;
return 1;
}
// attach/detach test
cout << "Testing attach/detach" << endl;
time(&start);

@ -2,6 +2,7 @@
SET(PROJECT_HDRS
DFCommonInternal.h
DFError.h
DFHackAPI.h
DFMemInfo.h
DFMemInfoManager.h

@ -0,0 +1,55 @@
/*
www.sourceforge.net/projects/dfhack
Copyright (c) 2009 Petr Mrázek (peterix), Kenneth Ferland (Impaler[WrG]), dorf
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifndef ERROR_H_INCLUDED
#define ERROR_H_INCLUDED
#include <string>
#include <exception>
namespace DFHack
{
namespace Error
{
class NoProcess : public exception
{
public:
virtual const char* what() const throw()
{
return "couldn't find a suitable process";
}
};
class CantAttach : public exception
{
public:
virtual const char* what() const throw()
{
return "couldn't attach to process";
}
};
}
}
#endif // ERROR_H_INCLUDED

@ -23,6 +23,7 @@ distribution.
*/
#include "DFCommonInternal.h"
#include "DFError.h"
using namespace DFHack;
class API::Private
@ -1369,14 +1370,16 @@ bool API::Attach()
// find a process (ProcessManager can find multiple when used properly)
if (!d->pm->findProcessess())
{
cerr << "couldn't find a suitable process" << endl;
return false;
throw Error::NoProcess();
//cerr << "couldn't find a suitable process" << endl;
//return false;
}
d->p = (*d->pm) [0];
if (!d->p->attach())
{
cerr << "couldn't attach to process" << endl;
return false; // couldn't attach to process, no go
throw Error::CantAttach();
//cerr << "couldn't attach to process" << endl;
//return false; // couldn't attach to process, no go
}
d->offset_descriptor = d->p->getDescriptor();
// process is attached, everything went just fine... hopefully