try-catch

Try Catch on executable exe in Powershell?

不羁岁月 提交于 2019-12-18 03:17:07
问题 I want to do a Try Catch on an .exe in Powershell, what I have looks like this: Try { $output = C:\psftp.exe ftp.blah.com 2>&1 } Catch { echo "ERROR: " echo $output return } echo "DONE: " echo $output When I use say an invalid domain, it returns an error like psftp.exe : Fatal: Network error: Connection refused but my code is not catching that. How would I catch errors? 回答1: try / catch in PowerShell doesn't work with native executables. After you make the call to psftp.exe, check the

Policy with catching std::bad_alloc

◇◆丶佛笑我妖孽 提交于 2019-12-18 02:40:15
问题 So I use Qt a lot with my development and love it. The usual design pattern with Qt objects is to allocate them using new . Pretty much all of the examples (especially code generated by the Qt designer) do absolutely no checking for the std::bad_alloc exception. Since the objects allocated (usually widgets and such) are small this is hardly ever a problem. After all, if you fail to allocate something like 20 bytes, odds are there's not much you can do to remedy the problem. Currently, I've

Flatten Scala Try

安稳与你 提交于 2019-12-18 02:19:40
问题 Is there a simple way to flatten a collection of try's to give either a success of the try values, or just the failure? For example: def map(l:List[Int]) = l map { case 4 => Failure(new Exception("failed")) case i => Success(i) } val l1 = List(1,2,3,4,5,6) val result1 = something(map(l1)) result1: Failure(Exception("failed")) val l2 = List(1,2,3,5,6) val result2 = something(map(l2)) result2: Try(List(1,2,3,5,6)) And can how would you handle multiple Failures in the collection? 回答1: Maybe not

try catch statement in PHP where the file does not upload

泪湿孤枕 提交于 2019-12-17 22:56:50
问题 I understand what try-catch statements do, but from reading the documentation on php.net, I would not be able to implement one into my own code. I need a real example to help me understand. How can I turn this example into a try catch statement, if the upload was not successful? $move = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES['file']['name']); if (!$move) { die ('File didn't upload'); } else { //opens the uploaded file for extraction echo

Catch all possible android exception globally and reload application

让人想犯罪 __ 提交于 2019-12-17 22:23:41
问题 I know the best way to prevent system crashes is catching all possible exception in different methods. So I use try catch blocks every where in my code. However as you know sometimes you forget to test some scenarios which cause some unhanded exceptions and a user gets "Unfortunately App stopped working..." message. This is bad for any application. Unfortunately the people who will use my app are not native English, so they will not understand crash message too. So I want to know is it

unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown

余生颓废 提交于 2019-12-17 21:31:26
问题 I am creating a class -- just a class, no main() and I am receiving the error of "unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown" at this line: FileOutputStream outStr = new FileOutputStream(FILE, true); I don't understand; I put in a try{} catch{} block and it's still reporting the error. Additionally, it's also reporting an "illegal start of type" for the try and both catch lines, and it's also saying that ';' is expected for both catch lines. I

Forwarding an error in Swift

佐手、 提交于 2019-12-17 21:25:32
问题 Is there a better solution to forward a Swift error from one function to another? In the moment, I'm doing it like this: enum Error:ErrorType{ case Error1 case Error2 } func func1()throws{ do{ try func2() }catch Error.Error1{ throw Error.Error1 }catch Error.Error2{ throw Error.Error2 } } func func2()throws{ // proof something throw Error.Error1 } So, to forward an error, I need to catch all the errors and throw them again. Is there a better solution? 回答1: Yes: don't wrap it in a do ... catch

Python Try Catch Block inside lambda

风流意气都作罢 提交于 2019-12-17 18:46:06
问题 Is it possible to use try catch block inside of a lambda function. I need the lambda function to convert a certain variable into an integer, but not all of the values will be able to be converted into integers. 回答1: Nope. A Python lambda can only be a single expression. Use a named function. It is convenient to write a generic function for converting types: def tryconvert(value, default, *types): for t in types: try: return t(value) except (ValueError, TypeError): continue return default Then

Capturing multiple error messages from a single statement inside TRY CATCH

拜拜、爱过 提交于 2019-12-17 16:20:54
问题 I am running a batch of statements on several columns and tables and want to retrieve information on what errors occur. The statement is a type change (varchar to nvarchar) and when it fails, it seems to return 2 errors. Msg 5074, Level 16, State 1, Line 1 The object 'DF_XXX_YYY' is dependent on column 'YYY'. Msg 4922, Level 16, State 9, Line 1 ALTER TABLE ALTER COLUMN Description failed because one or more objects access this column. However, when I wrap it in a TRY/CATCH block, and select

java try finally block to close stream

拈花ヽ惹草 提交于 2019-12-17 16:01:53
问题 I want to close my stream in the finally block, but it throws an IOException so it seems like I have to nest another try block in my finally block in order to close the stream. Is that the right way to do it? It seems a bit clunky. Here's the code: public void read() { try { r = new BufferedReader(new InputStreamReader(address.openStream())); String inLine; while ((inLine = r.readLine()) != null) { System.out.println(inLine); } } catch (IOException readException) { readException