I’m getting an IndentationError. How do I fix it?

Why does indentation matter? In Python, indentation is used to delimit blocks of code. This is different from many other languages that use curly braces {} to delimit blocks such as Java, Javascript, and C. Because of this, Python users must pay close attention to when and how they indent their code because whitespace matters. … Read more

Understanding checked vs unchecked exceptions in Java

Many people say that checked exceptions (i.e. these that you should explicitly catch or rethrow) should not be used at all. They were eliminated in C# for example, and most languages don’t have them. So you can always throw a subclass of RuntimeException (unchecked exception) However, I think checked exceptions are useful – they are … Read more

Importing installed package from script raises “AttributeError: module has no attribute” or “ImportError: cannot import name”

This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name. An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name … Read more

What does “Fatal error: Unexpectedly found nil while unwrapping an Optional value” mean?

Background: What’s an Optional? In Swift, Optional<Wrapped> is an option type: it can contain any value from the original (“Wrapped”) type, or no value at all (the special value nil). An optional value must be unwrapped before it can be used. Optional is a generic type, which means that Optional<Int> and Optional<String> are distinct types … Read more

How to catch an exception in Java? [closed]

try { int userValue = Integer.parseInt(aString); } catch (NumberFormatException e) { //there you go } and specifically in your code: public void actionPerformed(ActionEvent e) { String str; int no; //———————————— try { //lots of ifs here } catch (NumberFormatException e) { //do something with the exception you caught } if (e.getSource() == finish) { if … Read more