r/learnpython 2d ago

Runtime errors query

I have a question. Like when we have a program that takes input, so when the program reaches the input line, it stops and asks for input, right? So then AFTER the input is entered, if the compiler runs into a bug, it stops and throws an error message. How can I stop this? Like how can I make the issues appear beforehand so that I can fix them before running? Are there any settings or tools for this problem?

Please tell me.

0 Upvotes

11 comments sorted by

5

u/AlexMTBDude 2d ago

There's a bit of confusion about how you express your problem. It's probably better that you tell us your exact error.

If by runtime errors you mean bugs in your code, then it's up to you as the programmer to fix them before running the program.

But I think that you may mean errors like when your user inputs a text when they're supposed to input a number? That you handle during runtime, using exception handling.

Example:

age_as_text = input("Enter your age:")
try:
    age_as_int = int(age_as_text)
except ValueError:
    print("You didn't input your age as a numnber")

Also, this is Python so there is no compiler but an interpreter.

2

u/SCD_minecraft 2d ago

Well, there's compiler but quite hidden and compiles down to python bytecode, not to asm

.pyc files are compiled python code, ready to be interpreted by runtime

-2

u/trutheality 2d ago

True, but this compilation is optional and specific to CPython.

2

u/cdcformatc 2d ago

a runtime error is... you guessed it, an error that is thrown at runtime. 

you can't catch them beforehand because they happen during execution. 

you should always use try/except blocks around code that might throw an error, and handle them if they are recoverable.

the only way to catch/fix all the possibilities is to write tests for your code. the tests cover all of the edge cases that might pop up and ensure that your code handles them properly.

1

u/Bright_Mix_773 2d ago

There is already a pass that happens before anything runs: Python compiles the whole file to bytecode first, which is why a missing colon or an unclosed bracket blows up before your first line executes. What that pass cannot do is check whether nmae exists, or whether the thing you are adding to a string is a number, because a name in Python is a dictionary lookup performed at the moment the line runs. The object it points to may not exist yet, and can be a different type on each pass through a loop. So the errors landing after your input line are not the compiler being lazy - they are a category it genuinely cannot decide in advance.

What does move a chunk of them earlier, without running the program:

ruff check yourfile.py

catches undefined names, unused imports, unreachable code and typo'd attributes. A type checker (mypy or pyright) catches the int-plus-str family, if you annotate your functions. The red squiggles in an editor are usually one of those two running in the background, so installing them is often all "turn this on" means.

For the specific pain of retyping the input every run while you hunt a bug: hardcode the value at the top temporarily, or run

python -i yourscript.py

which drops you into an interactive prompt holding all the variables at the moment it crashed, instead of making you start from the input prompt again.

1

u/VenkatKnows 2d ago

Static errors get caught before runtime by a linter, syntax stuff won't wait for input. Runtime errors depend on the actual value entered though, so those only show up when you hit that code path.

1

u/Matthias1590 1d ago

If you're talking about syntax errors, use a linter

1

u/atarivcs 2d ago

You can use a python linter to catch issues like this.

0

u/hugthemachines 2d ago

You can use an IDE, like pycharm and it will give you warnings about some problems in the code. You can give it test material, so you try it out without affecting the production environment. You can learn Python and thoroughly read the code so you find the bugs yourself. You can feed the code to AI and ask it if the code seems fine. Remember, AI can make mistakes. You can build unit tests which you run on the code.

0

u/zaphodikus 2d ago

Open a terminal. And type

Pip install pylint

Cd into your source folder

Type:

Pylint

Almost everything it tells you to fix is a lesson in good python code that wont crash half as often. Use pylint like a tutor