# Help me understand basic error checking

**URL:** <https://discuss.python.org/t/help-me-understand-basic-error-checking/20849>\
**Category:** Python Help\
**Created:** [November 8, 2022, 12:54am UTC](https://discuss.python.org/t/help-me-understand-basic-error-checking/20849 "2022-11-08T00:54:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Insomniac](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@Insomniac](https://discuss.python.org/u/Insomniac)\
**Post date:** [November 8, 2022, 12:54am UTC](https://discuss.python.org/t/help-me-understand-basic-error-checking/20849/1 "2022-11-08T00:54:51Z")

</div>

Trying to do some basic error checking. Only started with Python a few days ago. I’m confused why the following code does not work. No matter whether I start the program with 3 or 4 arguments, I always get the error message, and I don’t understand why that happens. Similar code has worked for me before in other programs. Please enlighten me what is happening here.

–code starts–

# some basic error checking

no\_third\_file = False  
try:  
outputfile = sys.argv[1]  
inputfile1 = sys.argv[2]  
inputfile2 = sys.argv[3]  
inputfile3 = sys.argv[4]  
except:  
outputfile = sys.argv[1]  
inputfile1 = sys.argv[2]  
inputfile2 = sys.argv[3]  
no\_third\_file = True  
finally:  
print(‘Not enough command line arguments found. Usage:’)  
print(sys.argv[0], ‘outputfile inputfile1 inputfile2 inputfile3’)  
print(‘Total output size is expected to be about two thirds of total input size’)  
print(‘If only two input files are available, the third can be omitted’)  
print(‘In that case, xor will be used to reconstruct the third file’)  
quit()

---

<div class="post-metadata">

**Author:** ![abessman](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/abessman/32/8367_2.png) [@abessman](https://discuss.python.org/u/abessman)\
**Post date:** [November 8, 2022, 8:56am UTC](https://discuss.python.org/t/help-me-understand-basic-error-checking/20849/2 "2022-11-08T08:56:11Z")

</div>

Please enclose your code in triple backticks to preserve formatting:

````auto
```
YOUR CODE HERE
```

````

The `finally` clause of a `try, except, finally` statement always runs, regardless of what happened in the `try` and `except` clauses. Since you put your error message in the `finally` clause, it will always be printed.

Oftentimes, you only need the `try` and `except` clauses, while the `finally` clause can (should) be omitted. It is commonly used to clean up resources that may not be properly released otherwise.

In your case, I would say that a `try, except` statement in unsuitable for the problem you are trying to solve. You have three cases:

1. Three command line arguments
2. Four command line arguments
3. Any other number of command line arguments

Distinguishing between these cases with a `try, except` statement cannot be done in a clean way, since the error case for case 2 and 3 is the same (`IndexError`). You would have to nest another `try, except` statement within the first `except` to catch the second error, which is not very nice. Furthermore, you won’t catch the error case where too many command line arguments were passed (if that should be considered as an error).

Instead, just use an `if` statement:

```python
outputfile = sys.argv[1]
inputfile1 = sys.argv[2]
inputfile2 = sys.argv[3]
if len(sys.argv) == 4:
    has_third_file = False
elif len(sys.argv) == 5
    inputfile3 = sys.argv[4]
    has_third_file = True
else:
    raise ValueError("Too many arguments")

```

Also, avoid naming booleans as negatives. `no_third_file=False` is a double negative which is harder to parse for someone reading the code than `has_third_file=True`.

On a related note, never do this:

```python
try:
    ...
except:
    ...

```

Always specify what exception you want to catch:

```python
try:
    ...
except IndexError:
    ...

```

A bare `except` catches anything and everything, and will hide bugs from you.
