Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I cause an exception on purpose in python?

So this is a little bit of a strange question, but it could be fun!

I need to somehow reliably cause an exception in python. I would prefer it to be human triggered, but I am also willing to embed something in my code that will always cause an exception. (I have set up some exception handling and would like to test it)

I've been looking around and some ideas appear to be division by zero or something along those lines will always cause an exception--Is there a better way? The most ideal would be to simulate a loss of internet connection while the program is running....any ideas would be great!

Have fun!

like image 734
Mizmor Avatar asked Jul 17 '12 16:07

Mizmor


1 Answers

Yes, there is: You can explicitly raise your own exceptions.

raise Exception("A custom message as to why you raised this.")

You would want to raise an appropriate exception/error for loss of network connectivity.

like image 199
Makoto Avatar answered Sep 23 '22 04:09

Makoto