exception-handling

Why is else clause needed for try statement in python? [duplicate]

独自空忆成欢 提交于 2019-12-06 20:09:30
问题 This question already has answers here : Python try-else (19 answers) Closed 5 years ago . In Python the try statement supports an else clause, which executes if the code in try block does not raise an exception. For example: try: f = open('foo', 'r') except IOError as e: error_log.write('Unable to open foo : %s\n' % e) else: data = f.read() f.close() Why is the else clause needed? Can't we write the above code as follows : try: f = open('foo', 'r') data = f.read() f.close() except IOError as

Continue loop iteration after exception is thrown

99封情书 提交于 2019-12-06 20:02:28
问题 Let's say I have a code like this: try { for (int i = 0; i < 10; i++) { if (i == 2 || i == 4) { throw new Exception("Test " + i); } } } catch (Exception ex) { errorLog.AppendLine(ex.Message); } Now, it's obvious that the execution will stop on i==2 , but I want to make it finish the whole iteration so that in the errorLog has two entries (for i==2 and i==4 ) So, is it possible to continue the iteration even the exception is thrown ? 回答1: Just change the scope of the catch to be inside the

Why does XmlDocument.LoadXml throw System.Net.WebException?

放肆的年华 提交于 2019-12-06 19:50:29
问题 Why does System.Xml.XmlDocument.LoadXml method throw System.Net.WebException ? This is really mind boggling crazy, if MSDN was right, LoadXml should at most give me a System.Xml.XmlException . Yet I have weird exceptions like: The underlying connection was closed: The connection was closed unexpectedly. Dim document As New XmlDocument document.LoadXml("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><x></x>") MsgBox

How to catch any exception (System.Exception) without a warning in F#?

大兔子大兔子 提交于 2019-12-06 17:55:40
问题 I tried to catch an Exception but the compiler gives warning: This type test or downcast will always hold let testFail () = try printfn "Ready for failing..." failwith "Fails" with | :? System.ArgumentException -> () | :? System.Exception -> () The question is: how to I do it without the warning? (I believe there must be a way to do this, otherwise there should be no warning) Like C# try { Console.WriteLine("Ready for failing..."); throw new Exception("Fails"); } catch (Exception) { } 回答1: C#

why does pthread_exit throw something caught by ellipsis?

别说谁变了你拦得住时间么 提交于 2019-12-06 17:39:08
问题 if the function called by pthread_create has the following structure try{ ...code.... pthread_detach(pthread_self()); pthread_exit(NULL); }catch(...){ std::cout<<"I am here"<<std::endl; } why does the exception handler for ellipsis get called at the execution of pthread_exit ? (note that std::exception , for instance, are not thrown) 回答1: At least in GCC pthread_exit might throw an ___forced_unwind exception, that is used for unwinding the stack during the thread exit. It does not inherit

Why does LINQ query throw an exception when I attempt to get a count of a type

≯℡__Kan透↙ 提交于 2019-12-06 17:33:23
问题 public readonly IEnumerable<string> PeriodToSelect = new string[] { "MONTH" }; var dataCollection = from p in somedata from h in p.somemoredate where h.Year > (DateTime.Now.Year - 2) where PeriodToSelect.Contains(h.TimePeriod) select new { p.Currency, h.Year.Month, h.Value }; Can someone tell me why an exception is thrown when at the following line of code? int count = dataCollection.Count(); This is the exception: System.NullReferenceException: Object reference not set to an instance of an

Why does this Jython loop fail after a single run?

前提是你 提交于 2019-12-06 17:30:34
问题 I've got the following code: public static String getVersion() { PythonInterpreter interpreter = new PythonInterpreter(); try { interpreter.exec(IOUtils.toString(new FileReader("./Application Documents/Scripts/Version.py"))); PyObject get_version = interpreter.get("get_latest_version"); PyObject result = get_version.__call__(interpreter.get("url")); String latestVersion = (String) result.__tojava__(String.class); interpreter.close(); return latestVersion; } catch (IOException ex) { ex

Why unhandled exception in a background thread doesnt crash the app domain?

心不动则不痛 提交于 2019-12-06 17:27:55
问题 I am completely puzzled. I was so sure that .NET shuts the whole application domain if there is uncaught exception in a thread that I never tested this. However I just tried the following code and it doesn't fail... Could anyone please explain why? (Tried in .NET 4 and 3.5) static void Main(string[] args) { Console.WriteLine("Main thread {0}", Thread.CurrentThread.ManagedThreadId); Action a = new Action(() => { Console.WriteLine("Background thread {0}", Thread.CurrentThread.ManagedThreadId);

Handle specific exception type in python

淺唱寂寞╮ 提交于 2019-12-06 17:11:50
问题 I have some code that handles an exception, and I want to do something specific only if it's a specific exception, and only in debug mode. So for example: try: stuff() except Exception as e: if _debug and e is KeyboardInterrupt: sys.exit() logging.exception("Normal handling") As such, I don't want to just add a: except KeyboardInterrupt: sys.exit() because I'm trying to keep the difference in this debug code minimal 回答1: Well, really, you probably should keep the handler for KeyboardInterrupt

Get xhr object in vb.net while ajax calling fails

筅森魡賤 提交于 2019-12-06 16:25:31
I have a big problem in jQuery.ajax call. I am calling the web service whenever click the update button. I have a separate web service class, in which consist of few methods. When I calling the web service method, I have made the error handling and log the error information in db after that I have to override the “ex” that means error object to XMLHttpRequest . Is it possible to assign the SqlException to ajax object ( xhr ) in VB.NET ? Please help me its much more useful for me. Yes it is possible! I try to describe it in VB.NET (mostly I use C#, but I hope I'll not made syntax errors). Let