Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
MySQLdb を使用してデータベースに接続し、クエリを実行しています。ただし、パスワードが正しくないか、MySQLdb がデータベースへの接続に失敗した場合、スクリプトは実行を停止します。代わりに、単純にエラー メッセージを文字列として受け取り、スクリプトを続行したいと考えています (クエリの完了は、スクリプトの他の部分にとって重要ではないため)。これは可能ですか?
.connect()呼び出しで例外が発生するため、スクリプトの実行が停止します。
.connect()
,ハンドラーでその例外をキャッチできます。try:except:
try:
except:
import MySQLdb try: connection = MySQLdb.connect(...) except MySQLdb.Error as ex: print "The connection failed: {}".format(ex)