try-catch

Simulating finally block in C++0x

瘦欲@ 提交于 2019-12-21 17:42:59
问题 Inspired from the other topic, I wrote this code which simulates a finally block: #include <cassert> #include <iostream> struct base { virtual ~base(){} }; template<typename TLambda> struct exec : base { TLambda lambda; exec(TLambda l) : lambda(l){} ~exec() { lambda(); } }; class lambda{ base *pbase; public: template<typename TLambda> lambda(TLambda l): pbase(new exec<TLambda>(l)){} ~lambda() { delete pbase; } }; class A{ int a; public: void start(){ int a=1; lambda finally = [&]{a=2; std:

Powershell error handling: do something if NO error occured

泪湿孤枕 提交于 2019-12-21 11:35:51
问题 I've been searching around for this but can't seem to find it. I'm having a script with a try {} catch {} statement. I'd like to add an action if NO error occured. Eg try { something } catch { "Error occured" } if (!error) { "No Error Occured" } How can I test if no error occured in the statement? Thanks in advance Walter 回答1: Chek the automatic-variable $error after cleared $error.clear() try { something } catch { "Error occured" } if (!$error) { "No Error Occured" } 回答2: Another way:

C#: Equivalent of the python try/catch/else block

浪子不回头ぞ 提交于 2019-12-21 11:29:27
问题 In Python, there is this useful exception handling code: try: # Code that could raise an exception except Exception: # Exception handling else: # Code to execute if the try block DID NOT fail I think it's useful to be able to separate the code that could raise and exception from your normal code. In Python, this was possible as shown above, however I can't find anything like it in C#. Assuming the feature or one like it doesn't exist, is it standard practice to put normal code in the try

Is it possible to do Vectored Strctured exception handling in c#?

删除回忆录丶 提交于 2019-12-21 06:25:19
问题 As far as i know, when a first chance exception occurs, the debugger is notified(if any) and then if still unhandled, the system searches for the nearest frame based exception handler in the stack if any. I was reading this link when I came to know about vectored exception handling. Question1) I was wondering if there is any way we can do that in managed code? Question2) I think that any try{}catch{} is a frame based handler but what happens when we register a handle at certain events like

I get a segmentation fault instead of an exception

两盒软妹~` 提交于 2019-12-21 04:51:14
问题 In the following code, at the first iteration I get an exception, and at the second one I get a segmentation fault with no error message printed. It seems the exception is not caught: int i = 0; while(i++ < 10) { try { cout << "Iteration: " << i << endl; // Code... cout << "OK" << endl; } catch(...) { cerr << "Error message" << endl; continue; } } Output: Iteration 1 Error message Iteration 2 Segmentation fault Is it normal, or there is something really wrong going on? In case it should be

PowerShell Try/Catch and Retry

混江龙づ霸主 提交于 2019-12-21 04:12:42
问题 I have a fairly large powershell scripts with many (20+) functions which perform various actions. Right now all of the code doesn't really have any error handling or retry functionality. If a particular task/function fails it just fails and continues on. I would like to improve error handling and implement retries to make it more robust. I was thinking something similar to this: $tries = 0 while ($tries -lt 5) { try{ # Do Something # No retries necessary $tries = 5; } catch { # Report the

Try/Catch and threading

≯℡__Kan透↙ 提交于 2019-12-21 03:51:10
问题 I have an idea why but I'd like to ask if someone has a good grasp on why the exception raised inside a thread is never caught by the code that started it. Here's some very simple code to demonstrate what I mean: using System; using System.Collections.Generic; using System.Threading; namespace TestCrash { class Program { private static void Crash(object control) { AutoResetEvent are = (AutoResetEvent)(((object[])control)[0]); are.Set(); throw new Exception("Burn baby burn"); } static void

try-catch doesn't work with XMLHTTPRequest

微笑、不失礼 提交于 2019-12-21 03:22:07
问题 I am trying to use the try-catch statements to handle the errors from XMLHTTPRequest , like below: var xhr = new XMLHttpRequest(); xhr.open('POST', someurl, true); try{ xhr.sendMultipart(object); } catch(err){ error_handle_function(); } When there was a 401 error thrown by xhr.sendMultipart , the error_handle_function was not called. Any idea how to fix this? Thanks! 回答1: I think you can't catch server errors that way. you should be checking the status code instead: var xhr = new

try-catch doesn't work with XMLHTTPRequest

眉间皱痕 提交于 2019-12-21 03:21:39
问题 I am trying to use the try-catch statements to handle the errors from XMLHTTPRequest , like below: var xhr = new XMLHttpRequest(); xhr.open('POST', someurl, true); try{ xhr.sendMultipart(object); } catch(err){ error_handle_function(); } When there was a 401 error thrown by xhr.sendMultipart , the error_handle_function was not called. Any idea how to fix this? Thanks! 回答1: I think you can't catch server errors that way. you should be checking the status code instead: var xhr = new

“TryParse / Parse like” pattern: what is the best way to implement it

只愿长相守 提交于 2019-12-20 21:54:06
问题 This question is a follow-up from How to indicate that a method was unsuccessful. The xxx() Tryxxx() pattern is something that can be very useful in many libraries. I am wondering what is the best way to offer both implementations without duplicating my code. What is best: public int DoSomething(string a) { // might throw an exception } public bool TrySomething(string a, out result) { try { result = DoSomething(a) return true; } catch (Exception) { return false; } or public int DoSomething