0

What does it mean to raise an exception?

The lesson did not define what "raise" means in this context.

15th Nov 2016, 4:34 AM
Kevin Kato
1 Answer
+ 2
Exceptions are special cases where whole program flow needs to be stopped or changed. Like if a number is to be divided by zero (ZeroDivisionError). You can create your own exceptions. For example, when user enters number instead of letter. At such times, if that case succeeds, you will have to raise an exception to let your main program know that user has entered number. You cannot check each and every time when user inputs data (in this case). For more understanding, check the below code. #this is from some module, say b def checkUserInput(number1): if(number1==0): raise ThisNumberIsNotValidError #this is custom Exception else: return number1 #lets just say i'm importing from b try{ userInput = b.checkUserInput(input()) } except ThisNumberIsNotValidError: print("You have entered Zero. Which is not valid") Hope this has solved your doubts.
15th Nov 2016, 5:03 AM
Nagendra Prajwal
Nagendra Prajwal - avatar