try-catch

How to catch an Exception like this on Flask?

北城以北 提交于 2020-01-01 09:13:07
问题 I run a simple flask app like this: from flask import Flask app = Flask(__name__) @app.route('/') def welcome(): return "OK" app.config.update( DEBUG = True ) if __name__ == '__main__': app.run(use_reloader = False) when I run it and visit it, sometimes(not always) it could't response the request and throw an except: Exception happened during processing of request from ('127.0.0.1', 54481) Traceback (most recent call last): File "c:\python27\Lib\SocketServer.py", line 295, in _handle_request

Removing excessive try-catch blocks

早过忘川 提交于 2020-01-01 04:47:13
问题 I'm refactoring a medium-sized WinForms application written by other developers and almost every method of every class is surrounded by a try-catch block. 99% of the time these catch blocks only log exceptions or cleanup resources and return error status. I think it is obvious that this application lacks proper exception-handling mechanism and I'm planning to remove most try-catch blocks. Is there any downside of doing so? How would you do this? I'm planning to: To log exceptions

Why does Python not implement the elif statement on try statement?

吃可爱长大的小学妹 提交于 2020-01-01 04:19:14
问题 So let's make a quick example. my_list = [ {"name": "toto", "value": 3}, {"name": "foo", "value": 42}, {"name": "bar", "value": 56} ] def foo(name): try: value = next(e["value"] for e in my_list if e["name"] == name) except StopIteration: print "Uuuh not found." else: if value % 2: print "Odd !" else: print "Even !" As you can see, the above code works : >>> foo("toto") Odd ! >>> foo("foo") Even ! >>> foo("kappa") Uuuh not found. I was just wondering if there is a particular reason about why

Catching versus Throwing Exceptions in Java [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-31 22:40:16
问题 This question already has answers here : When to catch the Exception vs When to throw the Exceptions? (8 answers) Closed 21 days ago . So I have two general questions about java in general. The first is when would one use a try/catch in the body of the method versus using throws exception in declaring the method? Here is a little demonstration of what I mean. This: public void whileChatting() throws IOException{} versus public void closeConnection() { try { } catch (IOException ioException) {

Throw exception vs Logging

风流意气都作罢 提交于 2019-12-31 17:53:30
问题 Is the following way to code good practice? try { //my code here } catch (Exception e) { logger.error("Some error ", e); throw new MyCustomException("Some error ", e); } Moreover, should I.. use only the logger? throw only the exception? do both? I understand that with throw I can catch the exception in another part of the callstack, but maybe additional logging has some hidden benefits and is useful as well. 回答1: I use both in some cases, logging and throwing the exception. Specially, it's

Throw exception vs Logging

谁说我不能喝 提交于 2019-12-31 17:53:10
问题 Is the following way to code good practice? try { //my code here } catch (Exception e) { logger.error("Some error ", e); throw new MyCustomException("Some error ", e); } Moreover, should I.. use only the logger? throw only the exception? do both? I understand that with throw I can catch the exception in another part of the callstack, but maybe additional logging has some hidden benefits and is useful as well. 回答1: I use both in some cases, logging and throwing the exception. Specially, it's

how to save exception in txt file?

浪尽此生 提交于 2019-12-31 09:11:30
问题 public DataTable InsertItemDetails(FeedRetailPL objFeedRetPL) { DataTable GetListID = new DataTable(); try { SqlParameter[] arParams = new SqlParameter[4]; arParams[0] = new SqlParameter("@Date", typeof(DateTime)); arParams[0].Value = objFeedRetPL.requestdate; } catch (Exception ex) { string dir = @"C:\Error.txt"; // folder location if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); File.AppendAllText(Server.MapPath("~/Error.txt"), "Message :" + ex.Message + "<br/>" + Environment

Is try/catch around whole C# program possible?

我只是一个虾纸丫 提交于 2019-12-31 08:57:13
问题 A C# program is invoked by: Application.Run (new formClass ()); I'd like to put a try/catch around the whole thing to trap any uncaught exceptions. When I put it around this Run method, exceptions are not caught; control only returns here when the program terminates after an uncaught exception. Where can I put try/catch to cover the whole program? Thanks! 回答1: To catch Windows Form's unhandled exceptions hook-up the AppDomain.UnhandledException and Application.ThreadException events. Of

Powershell Try Catch Continue

ぐ巨炮叔叔 提交于 2019-12-31 07:10:08
问题 I have a working powershell script, but when it cannot find a hostname, it throws a non-terminating exception and writes to the screen that that hostname was unable to be found. I want to catch that exception and simply write to the screen: Not Found. Then I want the script to carry on like normal. Here is the script: $listOfComputers = IMPORT-CSV test.txt $b = "2013-09-11" ForEach($computer in $listOfComputers){ $name = $computer.Name Write-Host $name -NoNewLine try{$reg = [Microsoft.Win32

How to cover block catch by JUnit with NoSuchAlgorithmException and KeyStoreException

久未见 提交于 2019-12-31 04:32:25
问题 I want to cover getKeyStore() methode, But I don't know how to cover catch block for NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException and CertificateException. My methode is : public static KeyManagerFactory getKeyStore(String keyStoreFilePath) throws IOException { KeyManagerFactory keyManagerFactory = null; InputStream kmf= null; try { keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());