exception-handling

is there a pythonic way to try something up to a maximum number of times?

ⅰ亾dé卋堺 提交于 2019-12-17 10:18:10
问题 I have a python script which is querying a MySQL server on a shared linux host. For some reason, queries to MySQL often return a "server has gone away" error: _mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away') If you try the query again immediately afterwards, it usually succeeds. So, I'd like to know if there's a sensible way in python to try to execute a query, and if it fails, to try again, up to a fixed number of tries. Probably I'd want it to try 5 times before

How to return a value in a Go function that panics?

我的梦境 提交于 2019-12-17 09:52:10
问题 My Go function is expected to return a value, but it may panic when calling a library function. I can use recover() to capture this in a deferred call, but how can I return a value in this case? func MyFunc() string{ defer func() { if err := recover(); err != nil { // What do I do to make MyFunc() return a value in case of panic? } }() SomeFuncThatMayPanic() return "Normal Return Value" // How can I return "ERROR" in case of panic? } 回答1: You can use named result parameters. Name your return

JAX-RS using exception mappers

三世轮回 提交于 2019-12-17 09:25:09
问题 I have read that I can create an implementation of javax.ws.rs.ext.ExceptionMapper that will map a thrown application exception to a Response object. I've created a simple example which throws an exception if the phone length is greater than 20 characters when persisting the object. I am expecting the exception to be mapped to an HTTP 400 (Bad Request) response; however, I am receiving an HTTP 500 (Internal Server Error) with the following exception: java.lang.ClassCastException: com.example

Why can't I catch a generic exception in C#?

旧巷老猫 提交于 2019-12-17 08:31:52
问题 I was doing some unit testing on code that could throw a number of exceptions depending on the inputs. So I tried something like the below code: (simplified for the example) static void Main(string[] args) { RunTest<ArgumentException>(); } static void RunTest<T>() where T : Exception, new() { try { throw new T(); //throw new ArgumentException(); <-- Doesn't work either } catch (T tex) { Console.WriteLine("Caught passed in exception type"); } catch (Exception ex) { Console.WriteLine("Caught

Obtain a std::ostream either from std::cout or std::ofstream(file)

谁说我不能喝 提交于 2019-12-17 08:24:47
问题 how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following: std::ostream out = condition ? &std::cout : std::ofstream(filename); I've seen some examples that are not exception-safe, such as one from http://www2.roguewave.com/support/docs/sourcepro/edition9/html/stdlibug/34-2.html: int main(int argc, char *argv

Obtain a std::ostream either from std::cout or std::ofstream(file)

荒凉一梦 提交于 2019-12-17 08:24:45
问题 how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following: std::ostream out = condition ? &std::cout : std::ofstream(filename); I've seen some examples that are not exception-safe, such as one from http://www2.roguewave.com/support/docs/sourcepro/edition9/html/stdlibug/34-2.html: int main(int argc, char *argv

javafx, update ui from another thread

风流意气都作罢 提交于 2019-12-17 07:25:12
问题 I have a javafx application, and a worker thread, implemented via javafx.concurrent.Task , that performs a long process, that is zipping and uploading a set of files. I've connected the task progress to a progress bar via progressProperty . In addition to this i want a detailed state about the item being processed to be reported into the ui. That is, the name of the file being processed along with its size and any error that may arise from the single file process. Updating the UI with these

How to check for valid xml in string input before calling .LoadXml()

守給你的承諾、 提交于 2019-12-17 07:23:12
问题 I would much prefer to do this without catching an exception in LoadXml() and using this results as part of my logic. Any ideas for a solution that doesn't involve manually parsing the xml myself? I think VB has a return value of false for this function instead of throwing an XmlException. Xml input is provided from the user. Thanks much! if (!loaded) { this.m_xTableStructure = new XmlDocument(); try { this.m_xTableStructure.LoadXml(input); loaded = true; } catch { loaded = false; } } 回答1:

Catching FULL exception message

六眼飞鱼酱① 提交于 2019-12-17 07:10:52
问题 Consider: Invoke-WebRequest $sumoApiURL -Headers @{"Content-Type"= "application/json"} -Credential $cred -WebSession $webRequestSession -Method post -Body $sumojson -ErrorAction Stop This throws the following exception: How can I catch it entirely or at least filter out the "A resource with the same name already exist."? Using $_.Exception.GetType().FullName yields System.Net.WebException and $_.Exception.Message gives The remote server returned an error: (400) Bad Request. 回答1: Errors and

Python: One Try Multiple Except

佐手、 提交于 2019-12-17 07:06:53
问题 In Python, is it possible to have multiple except statements for one try statement? Such as : try: #something1 #something2 except ExceptionType1: #return xyz except ExceptionType2: #return abc 回答1: Yes, it is possible. try: ... except FirstException: handle_first_one() except SecondException: handle_second_one() except (ThirdException, FourthException, FifthException) as e: handle_either_of_3rd_4th_or_5th() except Exception: handle_all_other_exceptions() See: http://docs.python.org/tutorial