
try:
with open('myfile.txt') as fh:
file_data = fh.read()
print(file_data)
except FileNotFoundError:
print('
データファイルがありません。
')
except PermissionError:
print('
許可されていません。
')
except Exception as err:
print('
他のエラーが発生しました。
', str(err))
import mysql.connector
class ConnectionError(Exception):
pass
class CredentialsError(Exception):
pass
class SQLError(Exception):
pass
class UseDatabase:
def __init__(self, config: dict):
self.configuration = config
def __enter__(self) -> 'cursor':
try:
self.conn = mysql.connector.connect(**self.configuration)
self.cursor = self.conn.cursor()
return self.cursor
except mysql.connector.errors.InterfaceError as err:
raise ConnectionError(err) ...