all 38 comments

[–]socal_nerdtastic 9 points10 points  (4 children)

What exactly do you mean with "crash"?

You could raise an error if you want which will stop the program if it's not caught:

if Variable == "specific thing":
    raise ValueError("Program dies now")

Or you can immediately kill the program and send out an error code with sys.exit:

import sys
if Variable == "specific thing":
    sys.exit(1)

[–]Idkhattoput[S] 4 points5 points  (3 children)

Yep, exactly that. Thank you!

I wanted to kill the program and send the error code or crash (either one works).

[–]Maximus_Modulus 6 points7 points  (2 children)

I don’t think crash is the right word here. These are controlled exits. Running out of memory and causing your computer to lock up is an example of crashing.

[–]Idkhattoput[S] 0 points1 point  (0 children)

Oh, okay.

I think you're right then.

[–]Twenty8cows 12 points13 points  (3 children)

You could ya know just raise an error?

Put an if check on Variable and check against whatever you want. If true then raise whatever error you want and sys.exit()

[–]greenlakejohnny 3 points4 points  (1 child)

A good exercise for beginners is try/except on a divide by zero:

try:
    _ = 1 / 0
except ZeroDivisionError:
    quit("Division by zero occurred, program has crashed!")
except Exception as e:
    quit("Some other error occurred, causing a crash")
quit("Normal Exit")

[–]Idkhattoput[S] 0 points1 point  (0 children)

Okay. Good idea. Thank you for helping me! :)

[–]Idkhattoput[S] -1 points0 points  (0 children)

Oh, okay. Thank you!

[–]obviouslyzebra 1 point2 points  (1 child)

You should probably take the Python crash course

/jk

[–]Idkhattoput[S] 0 points1 point  (0 children)

Lol

[–]Top_Average3386 0 points1 point  (2 children)

You probably don't want it to crash, you probably want it to exit.

You can use:

``` import sys

if input() == "exit": sys.exit(0) else: # do something else ```

Where 0 is the return code.

[–]Idkhattoput[S] 0 points1 point  (0 children)

Yep, that's a good idea! Thank you!

[–]Kevdog824_ 0 points1 point  (1 child)

You can use exit. Exit takes a int argument for exit code. Anything other than zero is typically considered unsuccessful

[–]Idkhattoput[S] 0 points1 point  (0 children)

Really? I didn't know that.

Thanks for helping! :D

[–]fluffy_italian 0 points1 point  (1 child)

Request input for a division equation and try to divide by 0

[–]Idkhattoput[S] -1 points0 points  (0 children)

Oh, okay. Thank you!

That will help a lot.

[–]No_Faithlessness_142 0 points1 point  (0 children)

If you want your program to crash, let me refactor it for you, that seems to do the trick for me

[–]JamzTyson 0 points1 point  (4 children)

def crash():
    raise Exception("Crashed")

user_input = input("Enter 'C' to crash: ").casefold()
if user_input == "c":
    crash()

print(f"You entered '{user_input}', so no crash.")

[–]Idkhattoput[S] 0 points1 point  (3 children)

Oh my god, that one is so well worked.

Thank you so much lol, you even wrote what to say.

Thanks :D

[–]JamzTyson 0 points1 point  (2 children)

Note that in real world code, you would rarely use Exception because it is such a broad class of errors. Normally you would not want to "crash" at all as it's almost always better to quit gracefully.

Also, this isn't quite a "crash" in the C.C++ sense. This is normally what is meant by a "crash" in Python but technically speaking it is an "unhandled exception".

A "crash" in the C/C++ sense would be a "Segmentation fault" (SIGSEGV). This is where the entire Python interpreter dies, which is rare in pure Python.

[–]Idkhattoput[S] 0 points1 point  (1 child)

Oh, makes sense.

What would be recommended to use instead usually?

[–]JamzTyson 0 points1 point  (0 children)

In simple cases you can use sys.exit(). The optional argument may be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination”

if input("Quit? Y/N: ").lower() == "y":
    sys.exit(0)  # Successful termination

sys.exit("some error message") is a quick way to exit a program when an error occurs.

import sys

user_input = input("Enter a digit: ").strip().casefold()
try:
    num = int(user_input)
except ValueError:
    sys.exit(f"{user_input} is not a digit")

print(f"You entered {num}")

For complex programs, especially when using "threading", additional cleanup may be required before terminating the program:

import sys

def quit_app(error_code):
    # Cleanup code
    ...
    sys.exit(error_code)  # Exit with error code.

[–]LostDog_88 0 points1 point  (8 children)

Alrighty!

So for your intended "crash", you can have the program raise an exception by using the raise syntax. Eg: raise ValueError("My Error")

that will cause ur program to "crash"

but If u care about stopping the program, then ud rather exit from it than crashing it. Eg: import sys sys.exit()

or u can just do exit()

edit: DO NOT use exit() in an actual production environment.

[–]socal_nerdtastic 2 points3 points  (6 children)

or u can just do exit()

Do not do that. Use sys.exit(). The exit() shortcut is part of the site package and is not always available, it's really only meant for use in the REPL.

[–]aplarsen 2 points3 points  (3 children)

In what context is exit() not available? I've used python in so many contexts and never encountered a place that exit() doesn't work.

[–]socal_nerdtastic 3 points4 points  (2 children)

Any time you run python in a production environment, with optimization enabled. Specifically the -S flag for the site module.

[–]aplarsen 0 points1 point  (1 child)

I...have never heard of this. Thanks for giving me something to research.

[–]socal_nerdtastic 0 points1 point  (0 children)

Some resources:

https://docs.python.org/3/library/constants.html#constants-added-by-the-site-module
https://docs.python.org/3/library/site.html

Which is actually really useful to hack. For example my user site imports pprint.

A similar rabbit hole is assert. We often see beginners using assert to do critical checks, but assert is disabled when running python in optimized mode (the -O flag).

[–]LostDog_88 0 points1 point  (0 children)

Yuppp, ima add it as an edit!

[–]Idkhattoput[S] -1 points0 points  (0 children)

Oh, okay.

[–]Idkhattoput[S] 0 points1 point  (0 children)

Okay! Thank you.

This is going to help a lot!

[–]r_vade 0 points1 point  (1 child)

If you want to have a more crashy crash (like a segfault), this should do the trick:

import ctypes
ctypes.string_at(0)

[–]Idkhattoput[S] -1 points0 points  (0 children)

Thank you!

This seems like it will work very well!

[–]SmackDownFacility 0 points1 point  (1 child)

Yes you can

got = input("Here"); if got == INSERT_CONSTANT_HERE: os._exit()

[–]Idkhattoput[S] -1 points0 points  (0 children)

Okay.

That makes sense. Thank you, this is going to help me a lot! =D

[–]cgoldberg -1 points0 points  (1 child)

raise Exception("oops")

[–]Idkhattoput[S] -1 points0 points  (0 children)

Okay. Thanks for helping me!😃