Why does an error in python shut down the whole program?

Hi there,
If there is an error in the Python code - why does the whole program shut down? and notify of a possible error?
In the example here there are two print() commands. If one is valid but the other is not the console only displays an error and does not ‘print’ the correct result.
Thanks in advance.

3 Likes

If something goes wrong in some sequence of steps that rely on each other, then there’s no meaningful way to continue, you want that to stop.
If it’s a bug then fix the bug.
If something was wrong about the outside world, then you might ignore the request that causes something to go wrong, and continue with the next one. Example: permission denied when opening a file -> don’t open that file, continue with next file, assuming that they are independent units of work and that it makes some kind of sense to continue.

5 Likes

I agree like in life if a machine a part of it is spiolt the rest will work unless it is a important part of the machine speaking as someone who understands basic syntax

Would there be any instances when you could bypass an error to keep the program running? If so, how and why?

Example code:

Parenthesis within parenthesis

print(“Then there he stood, surveying the crop fields before saying “We have an infestation.””)

print(‘line 1 \n\nline 3’)

Plus-Equal Operator

counter = 0
counter += 10
print(counter)

This is equivalent to

counter = 0
counter = counter + 10
print(counter)

The operator will also perform string concatenation

message = "Part 1 of message "
message += “Part 2 of message”
print(message)

Python ZeroDivisionError

numerator = 100
denominator = 0
bad_results = numerator / denominator

Modulo operations

zero = 8 % 4
print(zero)

nonzero = 12 % 5
print(nonzero)

String concatenation

first = "Hello "
second = “World”

result = first + second
print(result)

long_result = first + second + “!”
print(long_result)

print(“Hello World!”)

print(100)

pi = 3.14159
print(pi)

message = "The pressure is "

valve_pressure = 30

print(message + str(valve_pressure))

Greeting

message_string = “Hello there”
print(message_string)

Farewell

message_string = “Hasta la vista”
print(message_string)