How to Properly Ignore Exceptions

How to properly ignore exceptions

try:
doSomething()
except Exception:
pass

or

try:
doSomething()
except:
pass

The difference is that the second one will also catch KeyboardInterrupt, SystemExit and stuff like that, which are derived directly from BaseException, not Exception.

See documentation for details:

  • try statement
  • exceptions

However, it is generally bad practice to catch every error - see Why is "except: pass" a bad programming practice?

Python: How to ignore an exception and proceed?

except Exception:
pass

Python docs for the pass statement

How to ignore Exceptions in Java

There is no way to fundamentally ignore a thrown exception. The best that you can do is minimize the boilerplate you need to wrap the exception-throwing code in.

If you are on Java 8, you can use this:

public static void ignoringExc(RunnableExc r) {
try { r.run(); } catch (Exception e) { }
}

@FunctionalInterface public interface RunnableExc { void run() throws Exception; }

Then, and implying static imports, your code becomes

ignoringExc(() -> test.setSomething1(0));
ignoringExc(() -> test.setSomething2(0));

How to properly ignore exception in Java?

The only way to ignore an exception is to catch & swallow it, being very specific on the exception of course, you wouldn't want to catch Exception e, that would be a very bad idea.

try{
... //code
}
catch( VerySpecificException ignore){
Log(ignore);
}

Logging is obviously optional but a good practice.

How to ignore exceptions and proceed with whole blocks (multiple lines) in python?

This can be achieved with the fuckit python module.

Here is the code modified from your example:

#!/usr/bin/env python3
import fuckit

def one():
raise Exception
print( 'one' )
def two():
print( 'two' )

@fuckit
def helper():
one()
two()

helper()

And an example execution:

user@disp3221:~$ sudo python3 -m pip install fuckit
Collecting fuckit
Downloading https://files.pythonhosted.org/packages/cc/f4/0952081f9e52866f4a520e2d92d27ddf34f278d37204104e4be869c6911d/fuckit-4.8.1.zip
Building wheels for collected packages: fuckit
Running setup.py bdist_wheel for fuckit ... done
Stored in directory: /root/.cache/pip/wheels/a9/24/e6/a3e32536d1b2975c23ac9f6f1bdbc591d7b968e5e0ce6b4a4f
Successfully built fuckit
Installing collected packages: fuckit
Successfully installed fuckit-4.8.1
user@disp3221:~$

user@disp3221:~$ ./test.py
two
user@disp3221:~$

How to ignore all potential exceptions in Python?

Python has no way of doing that, and for good reasons.

It seems you're confused about what does it mean to write "robust" software: a robust program is not a program that is hard to kill and that will keep running no matter what, but a program that will handle edge cases properly. Keeping running is NOT enough... keeping running doing sensible things is the key point.

Unfortunately there's no way to do reasonable things automatically and you've to think on a case-by-case basis how to handle the error.

Beware that if a program has a lot of catch it's rarely a good program. Exceptions are meant to be raised in a lot of places and caught almost nowhere.

Note also that every catch is potentially a source of bugs... for example:

try:
print my_dict[foo()]
except KeyError:
...

cannot distinguish if the KeyError is coming for accessing a non-existing key in my_dict or if instead escaped from foo(). Rarely the two cases should be handled the same way...

Better is to write:

key = foo()
if key in my_dict:
print my_dict[key]
else:
...

so that only the side case of missing key in my_dict is handled and instead a KeyError exception will stop the program (stopping a program when you're not sure of what it's doing is the only reasonable thing to do).

Ignore exception in Python

pass is the keyword you are looking for.

How to ignore exceptions while looping?

Quick solution:

Catching the exceptions inside your loop.

for i in range(10):
try:
# Do kucoin stuff, which might raise an exception.
except Exception as e:
print(e)
pass

Adopting best practices:

Note that it is generally considered bad practice to catch all exceptions that inherit from Exception. Instead, determine which exceptions might be raised and handle those. In this case, you probably want to handle your Kucoin exceptions. (KucoinAPIException, KucoinResolutionException, and KucoinRequestException. In which case your loop should look like this:

for i in range(10):
try:
# Do kucoin stuff, which might raise an exception.
except (KucoinAPIException, KucoinRequestException, KucoinResolutionException) as e:
print(e)
pass

We can make the except clause less verbose by refactoring your custom exception hierarchy to inherit from a custom exception class. Say, KucoinException.

class KucoinException(Exception):
pass

class KucoinAPIException(KucoinException):
# ...

class KucoinRequestException(KucoinException):
# ...

class KucoinResolutionException(KucoinException):
# ...

And then your loop would look like this:

for i in range(10):
try:
# Do kucoin stuff, which might raise an exception.
except KucoinException as e:
print(e)
pass


Related Topics



Leave a reply



Submit