try-catch

How can I “try to do something and then detect if it fails” in bash?

余生长醉 提交于 2019-12-18 06:51:08
问题 In an answer to a previous question: How can I use 'do I have root access?' as a conditional in bash? The suggestion to 'try to do something and detect if it fails' instead of 'check permission and then do something' I have found plenty of rationale for this e.g.: Thorough use of 'if' statements or 'try/catch' blocks? What is the advantage of using try {} catch {} versus if {} else {} However, I have found very little clear information about how to implementing try/catch in bash. I would

Error Handling in Sybase

心已入冬 提交于 2019-12-18 06:48:29
问题 Is there a way to handle errors in SYBASE , such as the TRY-CATCH block you can use in MS SQL Server , Oracle , etc? I've searched the web and the only option I found was the global variable @@error , but it didn' work as I expected, for example, the following code: begin tran update table1 set name = 'new name' where name = 'old name' update table2 set id = 1 where id = 30 -- suppose id has a unique constraint and there's already a row with id = 1 IF @@error = 0 begin print 'commited' commit

JSLint error: Expected 'ignore' and instead saw 'ex'

馋奶兔 提交于 2019-12-18 05:35:10
问题 I use JSLint all the time, but just today, I came across an error that I've never seen before. With the following code, I got the error shown below: try { document.execCommand('BackgroundImageCache', false, true); } catch (ex) {} Error: Expected 'ignore' and instead saw 'ex'. } catch (ex) {} So I changed my code to the following, and the error went away: try { document.execCommand('BackgroundImageCache', false, true); } catch (ignore) {} I can't find any explanation on the Internet regarding

JSLint error: Expected 'ignore' and instead saw 'ex'

人盡茶涼 提交于 2019-12-18 05:35:02
问题 I use JSLint all the time, but just today, I came across an error that I've never seen before. With the following code, I got the error shown below: try { document.execCommand('BackgroundImageCache', false, true); } catch (ex) {} Error: Expected 'ignore' and instead saw 'ex'. } catch (ex) {} So I changed my code to the following, and the error went away: try { document.execCommand('BackgroundImageCache', false, true); } catch (ignore) {} I can't find any explanation on the Internet regarding

Recording SQL Server call stack when reporting errors

我只是一个虾纸丫 提交于 2019-12-18 04:54:26
问题 This is a follow up to the question Nested stored procedures containing TRY CATCH ROLLBACK pattern? In the catch block I use a stored procedure to report (reraise) the error by reading from ERROR_MESSAGE(), ERROR_PROCEDURE(), ERROR_LINE(), etc. As described here I also have a check so that it can determine if the error has already been rethrown (this happens with nested stored procedures as the error information is passed down through each TRY CATCH block). What I would like to do, either

Repetitive Try and Except Clauses

心不动则不痛 提交于 2019-12-18 04:41:54
问题 I've created a bunch of functions and I need very similar except clauses in all of them, but I hate having so many lines of try and except clauses and the same code inside of each function. For example: import sys import random def foo(): num=random.random() try: if num>0.5: print 'OK' elif num>0.25: raise NameError('Too Small') else: raise KeyboardInterrupt except NameError: print "%s had a NameError" % sys._getframe().f_code.co_name except: print "%s had a different Error" % sys._getframe()

Try Catch outside of: await Task.Run(()

有些话、适合烂在心里 提交于 2019-12-18 04:38:12
问题 Does try catch outside of: await Task.Run(() => make sense or just use them only inside of await? private async void Test() { try { await Task.Run(() => { try { DoingSomething(); } catch (Exception ex) { log.Error(ex.Message); } }); } catch (Exception ex) { log.Error(ex.Message); } } 回答1: If you handle Exception inside the delegate (in your case just for logging purpose), await will not raise an exception in normal circumstances. This should be fine. private async Task Test() { await Task.Run

How do I catch system-level exceptions in Linux C++?

↘锁芯ラ 提交于 2019-12-18 04:20:33
问题 The following catch() is not called: void test(void) { int i=1,j=0,k; try { k = i/j; } catch(...) { ...handle it... } } Is there a way to catch this kind of exception? 回答1: Please check http://linux.die.net/man/1/gcc there is a compiler option -mcheck-zero-division to handle this. Alternatively, installing a SIGFPE handler might be an option, A float div by 0 would then generate a 'FPE_ZERODIVIDE' signal(SIGFPE, (fptr) FPE_ExceptionHandler); void FPE_ExceptionHandler(int nSig,int nErrType,int

Wrap every method in try-catch or specific part of code

断了今生、忘了曾经 提交于 2019-12-18 04:14:42
问题 My senior colleague tells me to wrap every method within a try-catch block so they can trace where exceptions occurs to help debug issues quicker. Is it better to wrap every method in a Try Catch such as this to: Public int foo() { try { //do something }catch(Exeception ex) { //do something with ex } } Or is it better to catch exceptions where I think they may occur? E.g. doing something with an array may cause the IndexOutOfRangeException will occur. //wrap this in try catch int[] array =

PHP try-catch not working

徘徊边缘 提交于 2019-12-18 03:53:39
问题 try { $matrix = Query::take("SELECT moo"); //this makes 0 sense while($row = mysqli_fetch_array($matrix, MYSQL_BOTH)) //and thus this line should be an error { } return 'something'; } catch(Exception $e) { return 'nothing'; } However instead of just going to catch part and returning nothing it shows a warning Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in the line starting with while . I have never came up to using exceptions in php, but used them a lot