In Cython, if I don't add "except + " to c++ function declaration , and just do a try / catch on python side for RuntimeErrors, won't that work? - cython

if i declare a c++ function in a pxd file without except + at the end, but still do try/catch on my python calling code, except i catch everything or just catch 'RuntimeError', won't that have same effect as having except + ? According to http://docs.cython.org/en/latest/src/userguide/wrapping_CPlusPlus.html#exceptions, most of C++ exceptions will be converted to RuntimeError anyway even with except +.

No that will not work.
except + is the indicator that tells Cython to generate the code that converts C++ exceptions to a Python exception. Without it Cython will assume that the function cannot throw a C++ exception and the C++ exception will propagate through the Cython code breaking everything.

Related

Firebreath promise reject slicing exception

Is there a way to pass any meaningful message, not “std::exception” to the promise’s fail callback? In the sources I found the following “void FB::variantDeferred::reject(std::exception e) const” specification. It seems when reject is called with any exception derived from std::exception the slicing happens and the right exception’s message is lost. Is there any workaround but to pass error through success callback?
std::exception is simply a base class for creating exceptions in C++. You can use a number of different methods to pass a specific string back; for example, you could throw a std::runtime_error, which accepts a message.
You could also subclass std::exception and provide an implementation of std::exception::what which returns a useful string representation of what you want.
FireBreath 2.0 will use the error message from e.what() when it creates the Error object. You can find this in the code, if you're curious how that works:
NPAPI
ActiveX
FireWyrm (used for Native Messaging)

How error handling is done in Jcuda?

In CUDA we can get to know about errors simply by checking return type of functions such as cudaMemcpy(), cudaMalloc() etc. which is cudaError_t with cudaSuccess. Is there any method available in JCuda to check error for functions such as cuMemcpyHtoD(), cuMemAlloc(), cuLaunchKernel() etc.
First of all, the methods of JCuda (should) behave exactly like the corresponding CUDA functions: They return an error code in form of an int. These error codes are also defined in...
the cudaError class for the Runtime API
the CUresult class for the Driver API
the cublasStatus class for JCublas
the cufftResult class for JCufft
the curandStatus class for JCurand
the cusparseStatus class for JCusparse
and are the same error codes as in the respective CUDA library.
All these classes additionally have a static method called stringFor(int) - for example, cudaError#stringFor(int) and CUresult#stringFor(int). These methods return a human-readable String representation of the error code.
So you could do manual error checks, for example, like this:
int error = someCudaFunction();
if (error != 0= {
System.out.println("Error code "+error+": "+cudaError.stringFor(error));
}
which might print something like
Error code 10: cudaErrorInvalidDevice
But...
...the error checks may be a hassle. You might have noticed in the CUDA samples that NVIDIA introduced some macros that simplify the error checks. And similarly, I added optional exception checks for JCuda: All the libraries offer a static method called setExceptionsEnabled(boolean). When calling
JCudaDriver.setExceptionsEnabled(true);
then all subsequent method calls for the Driver API will automatically check the method return values, and throw a CudaException when there was any error.
(Note that this method exists separately for all libraries. E.g. the call would be JCublas.setExceptionsEnabled(true) when using JCublas)
The samples usually enable exception checks right at the beginning of the main method. And I'd recommend to also do this, at least during the development phase. As soon as it is clear that the program does not contain any errors, one could disable the exceptions, but there's hardly a reason to do so: They conveniently offer clear information about which error occurred, whereas otherwise, the calls may fail silently.

lua can't unwind exception, generated by luaL_error on hp-ux

I have a problem with lua... on hp-ux 11.31
I have a lua-script that call some function that has been written on C++.
In that function luaL_error is called... but application crashed then luaL_error is called, because exception isn't unwind by lua...
On other platforms this application is work correct.
Do you have any idea what may be wrong?
You probably need to compile the Lua library as a C++ library, instead of a C library. Then Lua will use C++ exceptions instead of longjmps.

Obtaining stack traces in ClojureScript

In my ClojureScript programs running in FireFox 5.0 on Ubuntu 10.04.1 LTS, I get a single cryptic line when an exception is thrown.
'Error: No protocol method ISeqable.-seq defined for type object: [object Object]' when calling method: [nsIDOMEventListener::handleEvent]
The "-seq" bit seems strange to me and I have searched the generated javascript files for it and not found it.
I hope I am not missing something entirely obvious, but how do I get a stack trace of the exception thrown? How are you debugging your scripts?
Unfortunately stack traces from errors rely on browser support. Most (all?) browsers will allow you to access a canned version of the stack trace (usually the top 10 elements, iirc) as a string by dereferencing the 'stack' field, so you could do something like this:
(try ...throws...
(catch js/Error e
(.log js/console (.-stack e))))
However, string stack traces aren't much fun, you can't click them to take you to the source. Better is printing the exception directory to the javascript console (if it's available) to print stack traces with clickable links. E.g.
(try ...throws...
(catch js/Error e
(.log js/console e)))
At least in chrome, this only works if the javascript console was open when the error was thrown. This is great for debugging, but less useful when the error was unexpected.
The javascript console objects provided by most browsers have lots of useful functions that you can use from clojurescript. If you want to get helpful line numbers though, you probably want to write a couple of macros to inject the code to print to the console, otherwise all your line numbers will point to your print function.
Looks like you are passing a Javascript object to a Clojurescript function which expects a Clojure sequence. Try (my-function (js->clj my-thing)) edit: or, I'm guessing you're using (.strobj) where you don't need to

Global Uncaught Exception Handler in VC++ Application?

Is there anyway to catch all uncaught exceptions in a MFC VC++ 2008 application? Is there something like this Java code:
Thread.currentThread().setUncaughtExceptionHandler(
new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
System.out.println("Oops! We have a exception in Thread '" + t.getName() + "': " + e.toString());
}
});
The handler should preferably catch all types of exceptions.
It depends what you are trying to catch. If you just want C++ exceptions then look at setting your own handlers using set_unexpected or set_terminate. If you want all Windows exceptions then you use SetUnhandledExceptionFilter to specify a top level handler.
Catching all Windows exceptions should in most cases catch all C++ exceptions as well but it is not always the case so you are best to use both approaches to catch as much as possible. There are some oddities with the last CRTs (see this) which might mean not all exceptions get caught.