try-catch
What’s better to use, a __try/__except block or a try / catch block?
They are two very different things. try/catch are the familiar C++ keywords you know. __try/__except is used to catch SEH exceptions. Exceptions raised by Windows itself, like DivisionByZero or AccessViolation. It is well described in the MSDN Library article for it. You can also use it to catch C++ exception because it leverages the Windows … Read more
Check if file is readable with Python: try or if/else?
A more explicit way to check if file is actually a file and not directory for example, and it is readable: from os import access, R_OK from os.path import isfile file = “/some/path/to/file” assert isfile(file) and access(file, R_OK), \ f”File {file} doesn’t exist or isn’t readable”
try..catch not catching async/await errors
400/500 is not an error, it’s a response. You’d only get an exception (rejection) when there’s a network problem. When the server answers, you have to check whether it’s good or not: try { let response = await fetch(‘not-a-real-url’) if (!response.ok) // or check for response.status throw new Error(response.statusText); let body = await response.text(); // … Read more
Variable scope and Try Catch in python
What’s wrong with the “else” clause ? for filename in files: try: im = Image.open(os.path.join(dirname,filename)) except IOError, e: print “error opening file :: %s : %s” % (os.path.join(dirname,filename), e) else: print im.size Now since you’re in a loop, you can also use a “continue” statement: for filename in files: try: im = Image.open(os.path.join(dirname,filename)) except IOError, … Read more
java: try finally blocks execution [duplicate]
When you return from try block, the return value is stored on the stack frame for that method. After that the finally block is executed. Changing the value in the finally block will not change the value already on the stack. However if you return again from the finally block, the return value on the … Read more
Java try/catch performance, is it recommended to keep what is inside the try clause to a minimum?
Is it recommended to keep the lines inside the try catch to bare minimum? No. Can’t imagine how you could think that the length of a try block or indeed of any block can have any impact on performance. Does the code inside the try clause run slower or cause any performance hit?. No. As … Read more
Main method code entirely inside try/catch: Is it bad practice?
Wrapping any piece of code in a try/catch block without a good reason is bad practice. In the .NET programming model, exceptions should be reserved for truly exceptional cases or conditions. You should only try to catch exceptions that you can actually do something about. Furthermore, you should should hardly ever catch the base System.Exception … Read more