pass这个关键字相当于一个占位符,好比TODO是一样的,只表示此行什么也不做,不代表其它的行代码不执行;
try: print(5/0) except ZeroDivisionError: pass print("ddd") #这行还是可以正常执行的
def parse_int(s): try: n = int(v) except Exception as e: print('Could not parse, Reason:', e) parse_int('30') ##Reason: name 'v' is not defined
try: client_obj.get_url(url) except (URLError, ValueError, SocketTimeout): client_obj.remove_url(url)
try: client_obj.get_url(url) except (URLError, ValueError): client_obj.remove_url(url) except SocketTimeout: client_obj.handle_url_timeout(url)
try: f = open(filename) except OSError: pass
class NetworkError(Exception): pass class HostnameError(NetworkError): pass class CustomError(Exception): def __init__(self, message, status): super().__init__(message, status) self.message = message self.status = status
try: msg = s.recv() except TimeoutError as e: print(e) except RuntimeError as e: print(e.args)
try: raise RuntimeError('It failed') #抛出新异常-raise Error except RuntimeError as e: print(e.args)
def example(): try: int('N/A') except ValueError: print("Didn't work") raise #捕获后再抛出
try: print(5/0) except ZeroDivisionError as e: print(e.args)
import warnings warnings.simplefilter('always') def func(x, y, log_file=None, debug=False): if log_file is not None: warnings.warn('log_file argument deprecated', DeprecationWarning) func(1, 2, 'a') #第一行日志输出warn内容,第二行输出代码内容 /Users/liudong/personCode/python/pythonTest/app/base/base_type.py:5: UserWarning: log_file argument deprecated warnings.warn('log_file argument deprecated')
import json; numbers = [2,3,4,5,6]; fileName = "numbers.json"; with open(fileName, "w") as fileObj: json.dump(numbers, fileObj); with open(fileName, "r") as fileObj: number1 = json.load(fileObj);
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
长按识别二维码并关注微信
更方便到期提醒、手机管理