exception-handling

c++ stacktrace from the function an exception is thrown?

喜欢而已 提交于 2019-12-10 14:49:49
问题 I can make use of gcc's backtrace to obtain a stack trace at any given point of a program, but I would like to obtain the trace from whatever frame the stack was in at the time an exception is thrown, ie prior to the stack unwinding. For instance, the following block func() { throw std::exception(); } try { func(); } catch ( std::exception ) { std::cout << print_trace(); //do stuff } ought to still be able to retain a frame for func() somehow. This has been asked before, but it involved an

Recommended way to handle problems/errors in algorithms

人盡茶涼 提交于 2019-12-10 14:49:19
问题 Keeping stacktrace out of it, lets say that the idea of 'error' is a problem that you didn't want to occur, but did. If I were to use a boolean system to check if the action successfully completed, it would look something like this: String[] array = new String[10]; int i = 0; public boolean accessValue(int id) { if(id < array.length) { //do something return true; } return false; } while(true) { if(!accessValue(i++)) { //tend to situation } } If I were to use Exceptions, it would look like

When to catch exceptions?

我的未来我决定 提交于 2019-12-10 14:48:43
问题 Would there be any difference If i do the following without using exceptions? void func() { try { if (n > 5) { throw "n is greater than 5"; } } catch (const char *e) { MessageBox(0, e, 0, 0); return; } } OR void func() { if (n > 5) { MessageBox(0, "n is greater than 5", "Error", 0); return; } } 回答1: I would probably say that you best advised not to use exceptions for flow control. Exceptions, as the name suggests, are for handling exceptional circumstances. In the above case you're clearly

Laravel 4 - Handling 404s With Custom Messages

半城伤御伤魂 提交于 2019-12-10 14:46:46
问题 According to Laravel 4 docs I can throw a 404 with a custom response: App::abort(404, 'My Message'); I can then handle all of my 404s with a custom page: App::missing(function($exception) { return Response::view('errors.missing', array(), 404); }); How can I pass 'My Message' through to the view in the same way that the generic Laravel error page does. Thanks! 回答1: You can catch your message through the Exception parameter App::missing(function($exception) { $message = $exception->getMessage(

Make Visual Studio break on User (std::exception) Exceptions?

纵饮孤独 提交于 2019-12-10 14:29:11
问题 My code throws unhandled exceptions, but the debugger in Visual Studio only breaks on those thrown by the system. For example, below the return value of getaddrinfo is not zero and my exception should be thrown first - in fact, if I place a breakpoint at line 171, it is hit - yet the debugger only breaks on the call to socket . I know I have to add my own types explicitly, or else check All C++ Exceptions not in this list, in Exception Settings , but this is a std::exception I am throwing,

Exception handling when errors may occur in main program or in cleanup

微笑、不失礼 提交于 2019-12-10 13:59:33
问题 This is with Python 2.6.6 (default) on Debian Squeeze. Consider the following Python code. import sys try: raise Exception("error in main") pass except: exc_info = sys.exc_info() finally: try: print "cleanup - always run" raise Exception("error in cleanup") except: import traceback print >> sys.stderr, "Error in cleanup" traceback.print_exc() if 'exc_info' in locals(): raise exc_info[0], exc_info[1], exc_info[2] print "exited normally" The error obtained is Error in cleanup Traceback (most

How to simplify/reuse this exception handling code

痞子三分冷 提交于 2019-12-10 13:59:29
问题 I tend to write code like the following a lot: BufferedWriter w = null; // Or any other object that throws exceptions and needs to be closed try { w = new BufferedWriter(new FileWriter(file)); // Do something with w } catch (IOException e) { e.printStackTrace(); } finally { if (w != null) { try { w.close(); } catch (IOException e) { e.printStackTrace(); } } } It usually involves an object that throws exceptions and needs to be closed, and that closing it may also throw an exception. I was

How to catch the Control-C exception in a Visual C++ console app?

守給你的承諾、 提交于 2019-12-10 13:48:49
问题 This seems like it ought to be simple, but I've tried both try {} catch (...) {} C++ exception handling and __try {} __finally {} structured exception handling (SEH) and neither one will catch the exception that happens when you Control-C the application. I didn't really expect C++ exception handling to do this, since the Control-C is a system-type signal and not caused by a C++ throw(), but I tried it anyway when SEH didn't work. If I run under a debugger, it shows that Control-C raises a

Finally block in try/catch not working?

五迷三道 提交于 2019-12-10 13:46:27
问题 Ok, as far as I understand, try/catch blocks try an operation and catch blocks catch exceptions. The more specific exceptions go up top, the more generic towards the bottom of the series of catch blocks. In the following code, I implement try/catch, everything works fine. As far as I understand, a finally block always executes. Some people have argued that there is no purpose to finally block, because if there is an exception or there isn't, the code after the last catch block gets executed

Is it possible to catch exceptions of renderer processes in electrons main process?

核能气质少年 提交于 2019-12-10 13:38:11
问题 I'm using Electrons Quick Start Projekt (Commit dbef48ee7d072a38724ecfa57601e39d36e9714e) to test exceptions. In index.html I changed the name of the required module from renderer.js to rendererXXX.js . require('./renderer.js') which results in an expected Exeption (it is visible in the devtools for that window): Uncaught Error: Cannot find module './rendererXXX.js' Now it would be nice if the main-process (see main.js ) is aware that one renderer process failed. Thus I wrapped the