Skip to content

Commit

Permalink
ShogunException should be a std::exception type
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Feb 21, 2018
1 parent 6928d63 commit 77f0d29
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 50 deletions.
4 changes: 2 additions & 2 deletions examples/undocumented/libshogun/classifier_mklmulticlass.cpp
@@ -1,7 +1,7 @@
/*
* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Authors: Soeren Sonnenburg, Thoralf Klein, Evgeniy Andreev, Alexander Binder,
* Authors: Soeren Sonnenburg, Thoralf Klein, Evgeniy Andreev, Alexander Binder,
* Sergey Lisitsyn
*/

Expand Down Expand Up @@ -349,7 +349,7 @@ int main()
}
catch(ShogunException & sh)
{
printf("%s",sh.get_exception_string());
printf("%s",sh.what());
}

exit_shogun();
Expand Down
2 changes: 1 addition & 1 deletion examples/undocumented/libshogun/library_mldatahdf5.cpp
Expand Up @@ -33,7 +33,7 @@ int main(int argc, char** argv)
}
catch (ShogunException& e)
{
SG_SWARNING("%s", e.get_exception_string());
SG_SWARNING("%s", e.what());
}

SG_FREE(mat);
Expand Down
2 changes: 1 addition & 1 deletion examples/undocumented/libshogun/neuralnets_basic.cpp
Expand Up @@ -69,7 +69,7 @@ int main(int, char*[])
catch (ShogunException e)
{
// out of memory
SG_SPRINT(e.get_exception_string());
SG_SPRINT(e.what());
return 0;
}

Expand Down
Expand Up @@ -76,7 +76,7 @@ int main(int, char*[])
catch (ShogunException e)
{
// out of memory
SG_SPRINT(e.get_exception_string());
SG_SPRINT(e.what());
return 0;
}

Expand Down
Expand Up @@ -65,7 +65,7 @@ int main(int, char*[])
catch (ShogunException e)
{
// out of memory
SG_SPRINT(e.get_exception_string());
SG_SPRINT(e.what());
return 0;
}

Expand Down
Expand Up @@ -62,7 +62,7 @@ int main(int, char*[])
catch (ShogunException e)
{
// out of memory
SG_SPRINT(e.get_exception_string());
SG_SPRINT(e.what());
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/python/CustomKernel_protocols.i
Expand Up @@ -562,7 +562,7 @@ static PyObject* class_name ## _cleanup_custom(PyObject *self, PyObject *args)
}
catch (shogun::ShogunException e)
{
SWIG_exception(SWIG_SystemError, const_cast<char*>(e.get_exception_string()));
SWIG_exception(SWIG_SystemError, const_cast<char*>(e.what()));

SWIG_fail;

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/python/DenseFeatures_protocols.i
Expand Up @@ -555,7 +555,7 @@ static PyObject* class_name ## _free_feature_matrix(PyObject *self, PyObject *ar
}
catch (shogun::ShogunException e)
{
SWIG_exception(SWIG_SystemError, const_cast<char*>(e.get_exception_string()));
SWIG_exception(SWIG_SystemError, const_cast<char*>(e.what()));

SWIG_fail;

Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/python/swig_typemaps.i
Expand Up @@ -1302,7 +1302,7 @@ TYPEMAP_SPARSEFEATURES_OUT(PyObject, NPY_OBJECT)

%typemap(throws) shogun::ShogunException
{
PyErr_SetString(PyExc_RuntimeError, $1.get_exception_string());
PyErr_SetString(PyExc_RuntimeError, $1.what());
SWIG_fail;
}

Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/swig/SGBase.i
Expand Up @@ -288,7 +288,7 @@ public void readExternal(java.io.ObjectInput in) throws java.io.IOException, jav
}
catch (shogun::ShogunException e)
{
SWIG_exception(SWIG_SystemError, const_cast<char*>(e.get_exception_string()));
SWIG_exception(SWIG_SystemError, const_cast<char*>(e.what()));
#if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
SWIG_fail;
#endif
Expand Down Expand Up @@ -429,7 +429,7 @@ namespace shogun
SG_SERROR("File contains an HDF5 stream but " \
"Shogun was not compiled with HDF5" \
" support! - cannot load file %s." \
" (exception was %s)", e.get_exception_string());
" (exception was %s)", e.what());
}
#endif
$self->load_serializable(fstream);
Expand Down
12 changes: 4 additions & 8 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -373,8 +373,7 @@ bool CSGObject::save_serializable(CSerializableFile* file,
catch (ShogunException& e)
{
SG_SWARNING("%s%s::save_serializable_pre(): ShogunException: "
"%s\n", prefix, get_name(),
e.get_exception_string());
"%s\n", prefix, get_name(), e.what());
return false;
}

Expand All @@ -396,8 +395,7 @@ bool CSGObject::save_serializable(CSerializableFile* file,
catch (ShogunException& e)
{
SG_SWARNING("%s%s::save_serializable_post(): ShogunException: "
"%s\n", prefix, get_name(),
e.get_exception_string());
"%s\n", prefix, get_name(), e.what());
return false;
}

Expand Down Expand Up @@ -430,8 +428,7 @@ bool CSGObject::load_serializable(CSerializableFile* file,
catch (ShogunException& e)
{
SG_SWARNING("%s%s::load_serializable_pre(): ShogunException: "
"%s\n", prefix, get_name(),
e.get_exception_string());
"%s\n", prefix, get_name(), e.what());
return false;
}
if (!m_load_pre_called)
Expand All @@ -452,8 +449,7 @@ bool CSGObject::load_serializable(CSerializableFile* file,
catch (ShogunException& e)
{
SG_SWARNING("%s%s::load_serializable_post(): ShogunException: "
"%s\n", prefix, get_name(),
e.get_exception_string());
"%s\n", prefix, get_name(), e.what());
return false;
}

Expand Down
35 changes: 16 additions & 19 deletions src/shogun/lib/ShogunException.cpp
Expand Up @@ -13,30 +13,27 @@

using namespace shogun;

void
ShogunException::init(const char* str)
ShogunException::ShogunException(const std::string& what_arg):
std::exception(),
msg(what_arg)
{
size_t n = strlen(str) + 1;

val = (char*) malloc(n);
if (val)
strncpy(val, str, n);
else {
fprintf(stderr, "Could not even allocate memory for exception"
" - dying.\n");
exit(1);
}
}

ShogunException::ShogunException(const char* str)
{
#ifndef WIN32
#endif

init(str);
ShogunException::ShogunException(const char* what_arg):
std::exception(),
msg(what_arg)
{
}

ShogunException::ShogunException(const ShogunException& orig)
{ init(orig.val); }
{ msg = orig.msg; }

ShogunException::~ShogunException()
{
}

ShogunException::~ShogunException() { free(val); }
const char* ShogunException::what() const noexcept
{
return msg.c_str();
}
23 changes: 13 additions & 10 deletions src/shogun/lib/ShogunException.h
Expand Up @@ -9,21 +9,28 @@

#include <shogun/lib/config.h>

#include <stdexcept>
#include <string>

namespace shogun
{
/** @brief Class ShogunException defines an exception which is thrown whenever an
* error inside of shogun occurs.
*/
class ShogunException
class ShogunException: public std::exception
{
void init(const char* str);

public:
/** constructor
*
* @param str exception string
*/
ShogunException(const char* str);
explicit ShogunException(const std::string& what_arg);

/** constructor
*
* @param str exception string
*/
explicit ShogunException(const char* what_arg);

/** copy constructor
*
Expand All @@ -35,15 +42,11 @@ class ShogunException
*/
virtual ~ShogunException();

/** get exception string
*
* @return the exception string
*/
inline const char* get_exception_string() { return val; }
virtual const char* what() const noexcept override;

private:
/** exception string */
char* val;
std::string msg;
};
}
#endif // _SHOGUN_EXCEPTION_H_
2 changes: 1 addition & 1 deletion src/shogun/statistical_testing/StreamingMMD.cpp
Expand Up @@ -161,7 +161,7 @@ void CStreamingMMD::Self::compute_kernel(ComputationManager& cm, std::vector<CFe
}
catch (ShogunException e)
{
SG_SERROR("%s, Try using less number of blocks per burst!\n", e.get_exception_string());
SG_SERROR("%s, Try using less number of blocks per burst!\n", e.what());
}
}
}
Expand Down

0 comments on commit 77f0d29

Please sign in to comment.