Pythonで例外処理を実行したいのですが、やり方がわからないので質問させていただきました。
input()で無効な値を入力したら例外処理でプログラムを終了したいのですがうまくいきません。
Python
1import imapclient, ssl, sys 2 3print("Please choose a number below to conitue") 4print("1. Gmail") 5print("2.Yahoo Japan Mail") 6print('3. For exit') 7mail_server = int(input()) 8 9try: 10 while True: 11 if mail_server > 0 and mail_server < 4: 12 break 13 print("Please enter valid number") 14 mail_server = int(input()) 15 if mail_server == 1: 16 print("Gmail") 17 elif mail_server == 2: 18 print("Yahoo Japan Mail") 19 elif mail_server == 3: 20 print("Exit") 21except Exception: 22 print("Invalid Character entered. Ending programme...") 23 exit() 24
上記のコードを実行すると以下のエラーが出ます。
print("Invalid Character entered. Ending programme...")をプリントしてプログラムを終了するのが本来やりたい動きです。
Traceback (most recent call last): File "mail_delete.py", line 7, in <module> mail_server = int(input()) ValueError: invalid literal for int() with base 10: '適当な列'
exceptの後ろにExceptionをつけると例外は全て拾ってくれると書いてあったのですが、
except 例外クラス1 :
except 例外クラス2 :
except:
のように例外の可能性のあるクラスをいくつも書かなくてはいけないのでしょうか?
追記:
制作途中のコードです。例外処理はまだ入れれておりませんので値を間違えるとエラーが起きて終了しますが、思った通りサーバーにログインしてメールの削除まではできました。あとはモードの数字もまだ変更できておりませんがよろしかったら意見いただけると嬉しいです。
python
1import imapclient, ssl, sys, pprint, imaplib 2 3imaplib._MAXLINE = 10000000 4 5# try: 6print("Please choose a number below to conitue") 7print("1. Gmail") 8print("2.Yahoo Japan Mail") 9print('3. For exit') 10 11while_enterd = True 12 13while while_enterd: 14 try: 15 mode = int(input("Number: ")) 16 if mode > 0 and mode < 4: 17 break 18 except Exception as e: 19 print("Value must be a number") 20 print("Please enter valid number") 21 22context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) 23 24if mode == 1: 25 print("Gmail is chosen") 26 imap_obj = imapclient.IMAPClient("imap.gmail.com", ssl=True, ssl_context=context) 27 print('Please type your email of Google account that ends with "@gmail.com"') 28 mail_ad = input("email:") 29 print('Please type your App password for gmail') 30 mail_pass = input("App password:") 31 try: 32 imap_obj.login(mail_ad, mail_pass) 33 except Exception as e: 34 print("Error message: " + e) 35 print("If you haven't created App password on your account yet,") 36 print("please refer to https://support.google.com/accounts/answer/185833?hl=en and set App password before you run this programme") 37 exit() 38 print("Logged in successfully") 39 # pprint.pprint(imap_obj.list_folders()) # To return folders of mail in list 40 imap_obj.select_folder("Inbox", readonly=False) 41 enterd = True 42 43 print("Please type an email address that you want to delete from your inbox") 44 delete_ad = input("email:") 45 while enterd: 46 UIDs = imap_obj.search(["FROM", delete_ad]) 47 number_of_mail = len(UIDs) 48 print("There are " + str(number_of_mail) + " mails. Would you like to delte them all?") 49 answer = input("y/n?:") 50 if answer.lower() == "y": 51 print("Deleting all the mails. This may take for a while...") 52 for delete_id in UIDs: 53 imap_obj.delete_messages(delete_id) 54 else: 55 imap_obj.logout() 56 print("Logging out from the mail server") 57 exit() 58 imap_obj.expunge() 59 print("Mails delted successfully") 60 print("Would you like to delete other mails?") 61 answer = input("y/n?:") 62 if answer.lower() == "y": 63 print("Please type an email address that you want to delete from your inbox") 64 delete_ad = input("email:") 65 else: 66 imap_obj.logout() 67 print("Logging out from the mail server") 68 exit() 69 imap_obj.logout() 70 71 72elif mode == 2: 73 print("Yahoo Japan Mail is chosen") 74 imap_obj = imapclient.IMAPClient("imap.mail.yahoo.co.jp", ssl=True, ssl_context=context) 75 print('Please type your email of Yahoo account that ends with "@yahoo.co.jp"') 76 mail_ad = input("email:") 77 print('Please type your password for Yahoo Japan Mail') 78 # print("If you haven't created App password on your account yet,") 79 # print("please refer to https://support.google.com/accounts/answer/185833?hl=en and set App password before you run this programme") 80 mail_pass = input("Password:") 81 # Insert Exception later 82 imap_obj.login(mail_ad, mail_pass) 83 print("Logged in successfully") 84 imap_obj.select_folder("Inbox", readonly=False) 85 86 enterd = True 87 print("Please type an email address that you want to delete from your inbox") 88 delete_ad = input("email:") 89 while enterd: 90 UIDs = imap_obj.search(["FROM", delete_ad]) 91 number_of_mail = len(UIDs) 92 print("There are " + str(number_of_mail) + " mails. Would you like to delte them all?") 93 answer = input("y/n?:") 94 if answer.lower() == "y": 95 print("Deleting all the mails. This may take for a while...") 96 for delete_id in UIDs: 97 imap_obj.delete_messages(delete_id) 98 else: 99 imap_obj.logout() 100 print("Logging out from the mail server") 101 exit() 102 imap_obj.expunge() 103 print("Mails delted successfully") 104 print("Would you like to delete other mails?") 105 answer = input("y/n?:") 106 if answer.lower() == "y": 107 print("Please type an email address that you want to delete from your inbox") 108 delete_ad = input("email:") 109 else: 110 imap_obj.logout() 111 print("Logging out from the mail server") 112 exit() 113 imap_obj.logout() 114elif mode == 3: 115 print("Exit") 116 117 118 119# except Exception as e: 120# print("Invalid Character entered. Ending programme...") 121# print(e) 122# exit() 123 124 125 126
よろしくお願いします。
回答2件
あなたの回答
tips
プレビュー