0

My code doesn't pass all sololearn test case For Python Exception and raise ErrorSpecifief. Please help me.

try: tweet = input() #your code goes here length = len(tweet) if length > 42: ValueError except ValueError: raise ValueError("Invalid length") # if error' occurs here due to Value Error' as specified, please look at carefuly, that python Will execute raise ValueError("Invalid length") and stop here. It won't execute the instruction line after rasise ValueError. print("Posted")

1st Oct 2023, 4:09 AM
Oliver Pasaribu
5 Respuestas
+ 1
You need the `raise` keyword to raise an exception. Syntax: raise [EXCEPTION_NAME]
1st Oct 2023, 4:35 AM
Dragon RB
Dragon RB - avatar
+ 1
Already. raised. 2/5 only. In simple manner, this probably like this. try: tweets = input() if len(tweets)<=42: print("Posted") else print("Invalid length") Python rules: try: tweets = input() if len(tweets)<=42: print("Posted") else raise ValueError("Invalid length") I think raise ValueError("err_Message") is very similar to print("err_Message) plus AUTOMATIC control transfer statement to immedietely termination of program flow due to an specifed error'? Furthermore, we can write: raise ValueError("err_message) without other additional code.
1st Oct 2023, 6:21 AM
Oliver Pasaribu
+ 1
Olena Meshcheriakova [**PLEASE INCLUDE THE DESCRIPTION OF THE CHALLENGE YOU GOT STUCK AT AS WELL SO WE CAN HELP YOU 100%! Also, avoid posting codes in the description of your questions or titles, but prefer send the link to your code so we can see and run the code!**] The raise keyword is not similar to print statement. The `raise` keyword raises an error, while the print statement simply prints the object out. Control flow statement like if-else cannot handle exceptions when an exception occurs.
1st Oct 2023, 6:48 AM
Dragon RB
Dragon RB - avatar
+ 1
tweets = input() try: if len(tweets) > 42: raise ValueError("Invalid length") except: print("Error") else: print("Posted")
15th Feb 2024, 9:34 AM
Mugabi David
Mugabi David - avatar
1st Oct 2023, 9:21 AM
Oliver Pasaribu