-
Notifications
You must be signed in to change notification settings - Fork 0
/
pythonadv_OOP_4.html
1369 lines (1216 loc) · 115 KB
/
pythonadv_OOP_4.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Python进阶(四)异常 | BEIDAO.</title><meta name="keywords" content="面向对象编程,Python进阶"><meta name="author" content="Beidaos"><meta name="copyright" content="Beidaos"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#C6B3B1"><meta name="mobile-web-app-capable" content="yes"><meta name="apple-touch-fullscreen" content="yes"><meta name="apple-mobile-web-app-title" content="Python进阶(四)异常"><meta name="application-name" content="Python进阶(四)异常"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="default"><link rel="bookmark" href="/img/siteicon/apple-touch-icon.png"><link rel="apple-touch-icon-precomposed" sizes="180x180" href="/img/siteicon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="192x192" href="/img/siteicon/apple-touch-icon.png"><link rel="apple-touch-icon" sizes="512x512" href="/img/siteicon/apple-touch-icon.png"><link rel="apple-touch-startup-image" media="screen and (device-width:320px) and (device-height:568px) and (-webkit-device-pixel-ratio:2) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_1136x640.png"><link rel="apple-touch-startup-image" media="screen and (device-width:320px) and (device-height:568px) and (-webkit-device-pixel-ratio:2) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_640x1136.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:896px) and (-webkit-device-pixel-ratio:3) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_2688x1242.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:896px) and (-webkit-device-pixel-ratio:2) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_1792x828.png"><link rel="apple-touch-startup-image" media="screen and (device-width:375px) and (device-height:812px) and (-webkit-device-pixel-ratio:3) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_1125x2436.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:896px) and (-webkit-device-pixel-ratio:2) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_828x1792.png"><link rel="apple-touch-startup-image" media="screen and (device-width:375px) and (device-height:812px) and (-webkit-device-pixel-ratio:3) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_2436x1125.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:736px) and (-webkit-device-pixel-ratio:3) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_1242x2208.png"><link rel="apple-touch-startup-image" media="screen and (device-width:414px) and (device-height:736px) and (-webkit-device-pixel-ratio:3) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_2208x1242.png"><link rel="apple-touch-startup-image" media="screen and (device-width:375px) and (device-height:667px) and (-webkit-device-pixel-ratio:2) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_1334x750.png"><link rel="apple-touch-startup-image" media="screen and (device-width:375px) and (device-height:667px) and (-webkit-device-pixel-ratio:2) and (orientation:portrait)" href="/img/siteicon/splashIcons/icon_750x1334.png"><link rel="apple-touch-startup-image" media="screen and (device-width:1024px) and (device-height:1366px) and (-webkit-device-pixel-ratio:2) and (orientation:landscape)" href="/img/siteicon/splashIcons/icon_2732x2048.png"><meta name="description" content="目标 了解异常 捕获异常 异常的else 异常finally 异常的传递 自定义异常 一. 了解异常 当检测到一个错误时,解释器就无法继续执行了,反而出现了一些错误的提示,这就是所谓的"异常"。 例如:以r方式打开一个不存在的文件。 open(&#x27;test.txt&#x27;, &#x27;r&#x27;) 二. 异常的写法 2.1 语法 try: 可能发生">
<meta property="og:type" content="article">
<meta property="og:title" content="Python进阶(四)异常">
<meta property="og:url" content="https://www.beidaoaz.top/pythonadv_OOP_4.html">
<meta property="og:site_name" content="BEIDAO.">
<meta property="og:description" content="目标 了解异常 捕获异常 异常的else 异常finally 异常的传递 自定义异常 一. 了解异常 当检测到一个错误时,解释器就无法继续执行了,反而出现了一些错误的提示,这就是所谓的"异常"。 例如:以r方式打开一个不存在的文件。 open(&#x27;test.txt&#x27;, &#x27;r&#x27;) 二. 异常的写法 2.1 语法 try: 可能发生">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/%E5%8D%9A%E5%AE%A2%E7%AB%99%E7%82%B9%E9%85%8D%E5%9B%BE/python-logo.6puefdh4wtk0.png">
<meta property="article:published_time" content="2023-07-08T00:00:00.000Z">
<meta property="article:modified_time" content="2024-03-01T07:02:12.400Z">
<meta property="article:author" content="Beidaos">
<meta property="article:tag" content="面向对象编程">
<meta property="article:tag" content="Python进阶">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/%E5%8D%9A%E5%AE%A2%E7%AB%99%E7%82%B9%E9%85%8D%E5%9B%BE/python-logo.6puefdh4wtk0.png"><link rel="shortcut icon" href="/private_img/web_logo.png"><link rel="canonical" href="https://www.beidaoaz.top/pythonadv_OOP_4"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//npm.elemecdn.com"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="xxx"/><meta name="baidu-site-verification" content="code-xxx"/><meta name="msvalidate.01" content="xxx"/><link rel="manifest" href="/manifest.json"/><meta name="msapplication-TileColor" content="var(--anzhiyu-main)"/><link rel="apple-touch-icon" sizes="180x180" href="/img/siteicon/128.png"/><link rel="icon" type="image/png" sizes="32x32" href="/img/siteicon/32.png"/><link rel="icon" type="image/png" sizes="16x16" href="/img/siteicon/16.png"/><link rel="mask-icon" href="/img/siteicon/128.png" color="#5bbad5"/><script>if ('serviceWorker' in navigator) {
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.addEventListener('controllerchange', function() {
location.reload()
})
}
window.addEventListener('load', function() {
navigator.serviceWorker.register('/service-worker.js')
})
}</script><link rel="stylesheet" href="css/index.css"><link rel="stylesheet" href="https://npm.elemecdn.com/[email protected]/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/@fancyapps/[email protected]/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
root: '/',
friends_vue_info: undefined,
navMusic: true,
algolia: undefined,
localSearch: {"path":"/search.xml","preload":true,"languages":{"hits_empty":"找不到您查询的内容:${query}"}},
translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简","rightMenuMsgToTraditionalChinese":"转为繁体","rightMenuMsgToSimplifiedChinese":"转为简体"},
noticeOutdate: {"limitDay":30,"position":"top","messagePrev":"本文上次更新距离今天已经过去","messageNext":"天, 文中内容可能已经过时,望周知。"},
highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":330},
copy: {
success: '复制成功',
error: '复制错误',
noSupport: '浏览器不支持'
},
relativeDate: {
homepage: false,
post: false
},
runtime: '天',
date_suffix: {
just: '刚刚',
min: '分钟前',
hour: '小时前',
day: '天前',
month: '个月前'
},
copyright: undefined,
lightbox: 'fancybox',
Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#3b70fc","bgDark":"#1f1f1f","position":"top-center"},
source: {
justifiedGallery: {
js: 'https://npm.elemecdn.com/[email protected]/dist/fjGallery.min.js',
css: 'https://npm.elemecdn.com/[email protected]/dist/fjGallery.css'
}
},
isPhotoFigcaption: false,
islazyload: true,
isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
title: 'Python进阶(四)异常',
isPost: true,
isHome: false,
isHighlightShrink: false,
isToc: true,
postUpdate: '2024-03-01 15:02:12'
}</script><noscript><style type="text/css">
#nav {
opacity: 1
}
.justified-gallery img {
opacity: 1
}
#recent-posts time,
#post-meta time {
display: inline !important
}
</style></noscript><script>(win=>{
win.saveToLocal = {
set: function setWithExpiry(key, value, ttl) {
if (ttl === 0) return
const now = new Date()
const expiryDay = ttl * 86400000
const item = {
value: value,
expiry: now.getTime() + expiryDay,
}
localStorage.setItem(key, JSON.stringify(item))
},
get: function getWithExpiry(key) {
const itemStr = localStorage.getItem(key)
if (!itemStr) {
return undefined
}
const item = JSON.parse(itemStr)
const now = new Date()
if (now.getTime() > item.expiry) {
localStorage.removeItem(key)
return undefined
}
return item.value
}
}
win.getScript = url => new Promise((resolve, reject) => {
const script = document.createElement('script')
script.src = url
script.async = true
script.onerror = reject
script.onload = script.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
script.onload = script.onreadystatechange = null
resolve()
}
document.head.appendChild(script)
})
win.getCSS = (url,id = false) => new Promise((resolve, reject) => {
const link = document.createElement('link')
link.rel = 'stylesheet'
link.href = url
if (id) link.id = id
link.onerror = reject
link.onload = link.onreadystatechange = function() {
const loadState = this.readyState
if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
link.onload = link.onreadystatechange = null
resolve()
}
document.head.appendChild(link)
})
win.activateDarkMode = function () {
document.documentElement.setAttribute('data-theme', 'dark')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#18171d')
}
}
win.activateLightMode = function () {
document.documentElement.setAttribute('data-theme', 'light')
if (document.querySelector('meta[name="theme-color"]') !== null) {
document.querySelector('meta[name="theme-color"]').setAttribute('content', '#C6B3B1')
}
}
const t = saveToLocal.get('theme')
const now = new Date()
const hour = now.getHours()
const isNight = hour <= 8 || hour >= 22
if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
else if (t === 'light') activateLightMode()
else activateDarkMode()
const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
if (asideStatus === 'hide') {
document.documentElement.classList.add('hide-aside')
} else {
document.documentElement.classList.remove('hide-aside')
}
}
const detectApple = () => {
if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
document.documentElement.classList.add('apple')
}
}
detectApple()
})(window)</script><link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin><link href="https://fonts.loli.net/css2?family=Noto+Serif+SC:wght@400;500;700&display=swap" rel="stylesheet"><link href="https://fonts.loli.net/css2?family=Ma+Shan+Zheng:wght@400;500;700&display=swap" rel="stylesheet"><link rel="stylesheet" href="/css/custom.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/background-page.css?1"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/zhheo/JS-Heo@main/tag-link/tag-link.css"><!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.min.css">
<link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-clock-anzhiyu/lib/clock.min.css" /><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://cdn.cbd.int/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"></head><body data-type="anzhiyu"><div id="web_bg"></div><div id="an_music_bg"></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header"><nav id="nav"><span id="blog_name"><div class="back-home-button" tabindex="-1"><i class="anzhiyufont anzhiyu-icon-grip-vertical"></i><div class="back-menu-list-groups"><div class="back-menu-list-group"><div class="back-menu-list-title">学术</div><div class="back-menu-list"><a class="back-menu-item" href="https://sci-hub.st/" rel="external nofollow noreferrer" title="Sci-hub" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/ravenround_hs.f9qogiemdnk.gif" alt="Sci-hub"/><span class="back-menu-item-text">Sci-hub</span></a><a class="back-menu-item" href="https://www.connectedpapers.com/" rel="external nofollow noreferrer" title="ConnetPapers" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/Connected Papers.avesmhofaig.jpg" alt="ConnetPapers"/><span class="back-menu-item-text">ConnetPapers</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">工具</div><div class="back-menu-list"><a class="back-menu-item" href="https://vercel.com/" rel="external nofollow noreferrer" title="Vercel" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/vercel.7910zc14sj40.png" alt="Vercel"/><span class="back-menu-item-text">Vercel</span></a><a class="back-menu-item" href="https://picx.xpoet.cn/" rel="external nofollow noreferrer" title="PicX" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/PicX.1erb7rkva5c0.ico" alt="PicX"/><span class="back-menu-item-text">PicX</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">网页</div><div class="back-menu-list"><a class="back-menu-item" href="https://www.bilibili.com/" rel="external nofollow noreferrer" title="BiliBili" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/bilibili.7fu3r06fhp80.svg" alt="BiliBili"/><span class="back-menu-item-text">BiliBili</span></a></div></div><div class="back-menu-list-group"><div class="back-menu-list-title">镜像站</div><div class="back-menu-list"><a class="back-menu-item" href="https://mirrors.tuna.tsinghua.edu.cn/" rel="external nofollow noreferrer" title="清华镜像站" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/logo-small.4bmf6842eso0.png" alt="清华镜像站"/><span class="back-menu-item-text">清华镜像站</span></a><a class="back-menu-item" href="https://developer.aliyun.com/mirror/?spm=a2c6h.25603864.0.0.7cff28b9BKDFoW" rel="external nofollow noreferrer" title="阿里云镜像站" target="_blank"><img class="back-menu-item-icon" src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/图标/阿里云.3j8goscmtmu0.svg" alt="阿里云镜像站"/><span class="back-menu-item-text">阿里云镜像站</span></a></div></div></div></div><a id="site-name" href="/index.html"><div class="title">BEIDAO.</div><i class="anzhiyufont anzhiyu-icon-house-chimney"></i></a></span><div class="mask-name-container"><center id="name-container"><a id="page-name" href="javascript:anzhiyu.scrollToDest(0, 500)" rel="external nofollow noreferrer">PAGE_NAME</a></center></div><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 文章</span></a><ul class="menus_item_child" style="left:-79px;"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><i class="anzhiyufont anzhiyu-icon-box-archive faa-tada" style="font-size: 0.9em;"></i><span> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><i class="anzhiyufont anzhiyu-icon-shapes faa-tada" style="font-size: 0.9em;"></i><span> 分类</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-biaoqiantags3"></use></svg><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 社交</span></a><ul class="menus_item_child" style="left:-31px;"><li><a class="site-page child faa-parent animated-hover" href="/link/"><i class="anzhiyufont anzhiyu-icon-link faa-tada" style="font-size: 0.9em;"></i><span> 友链</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/essay/"><i class="anzhiyufont anzhiyu-icon-lightbulb faa-tada" style="font-size: 0.9em;"></i><span> 闲言碎语</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 我的</span></a><ul class="menus_item_child" style="left:-79px;"><li><a class="site-page child faa-parent animated-hover" href="/music/?id=886726002&server=netease"><i class="anzhiyufont anzhiyu-icon-music faa-tada" style="font-size: 0.9em;"></i><span> 音乐馆</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/collect/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-dropboxdropbox4"></use></svg><span> 藏宝阁</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/album/"><i class="anzhiyufont anzhiyu-icon-images faa-tada" style="font-size: 0.9em;"></i><span> 相册集</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 关于</span></a><ul class="menus_item_child" style="left:17px;"><li><a class="site-page child faa-parent animated-hover" href="/about/"><i class="anzhiyufont anzhiyu-icon-paper-plane faa-tada" style="font-size: 0.9em;"></i><span> 关于博主</span></a></li></ul></div></div></div><div id="nav-right"><div class="nav-button" id="randomPost_button"><a class="site-page" onclick="toRandomPost()" title="随机前往一个文章" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-dice"></i></a></div><div class="nav-button" id="search-button"><a class="site-page social-icon search" href="javascript:void(0);" rel="external nofollow noreferrer" title="搜索内容 - 搜一搜,省时又省力"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span> 搜索</span></a></div><div class="nav-button" id="changeBg-button"><a class="site-page social-icon search" href="javascript:void(0);" rel="external nofollow noreferrer" onclick="toggleWinbox()" title="切换背景 - 换一个背景,换一种感觉"><i class="iconfont icon-qiehuankapian"></i></a></div><div class="nav-button" id="darkmode_navswitch"><a class="darkmode_switchbutton" type="button" href="javascript:void(0);" rel="external nofollow noreferrer" title="浅色和深色模式转换" onclick="rm.switchDarkMode()"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke" style="font-size: 1.3rem"></i></a></div><div class="nav-button" id="nav-totop"><a class="totopbtn" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i><span id="percent" onclick="anzhiyu.scrollToDest(0,500)">0</span></a></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-bars"></i></a></div></div></nav><div id="post-info"><div id="post-firstinfo"><div class="meta-firstline"><a class="post-meta-original">原创</a><span class="post-meta-categories"><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-inbox post-meta-icon"></i><a class="post-meta-categories" href="categories/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/">面向对象编程</a></span><span class="article-meta tags"><a class="article-meta__tags" href="tags/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/"><span><i class="iconfont icon-Tags"></i>面向对象编程</span></a><a class="article-meta__tags" href="tags/Python%E8%BF%9B%E9%98%B6/"><span><i class="iconfont icon-Tags"></i>Python进阶</span></a></span></div></div><h1 class="post-title">Python进阶(四)异常</h1><div id="post-meta"><div class="meta-firstline"><i class="iconfont icon-zuozhe"></i><span class="post-meta-author">安志</span><span class="post-meta-date"><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-calendar-days post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-07-08T00:00:00.000Z" title="发表于 2023-07-08 08:00:00">2023-07-08</time><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-history post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-03-01T07:02:12.400Z" title="更新于 2024-03-01 15:02:12">2024-03-01</time></span></div><div class="meta-secondline"><span class="post-meta-separator"></span><span class="post-meta-wordcount"><i class="anzhiyufont anzhiyu-icon-file-word post-meta-icon" title="文章字数"></i><span class="post-meta-label" title="文章字数">字数总计:</span><span class="word-count" title="文章字数">1.2k</span><span class="post-meta-separator"></span><i class="anzhiyufont anzhiyu-icon-clock post-meta-icon" title="阅读时长"></i><span class="post-meta-label" title="阅读时长">阅读时长:</span><span>4分钟</span></span><span class="post-meta-separator"></span><span class="post-meta-position" title="作者IP属地为北京"><i class="anzhiyufont anzhiyu-icon-location-dot"></i>北京</span></div></div></div><section class="main-hero-waves-area waves-area"><svg class="waves-svg" xmlns="http://www.w3.org/2000/svg" xlink="http://www.w3.org/1999/xlink" viewBox="0 24 150 28" preserveAspectRatio="none" shape-rendering="auto"><defs><path id="gentle-wave" d="M -160 44 c 30 0 58 -18 88 -18 s 58 18 88 18 s 58 -18 88 -18 s 58 18 88 18 v 44 h -352 Z"></path></defs><g class="parallax"><use href="#gentle-wave" x="48" y="0"></use><use href="#gentle-wave" x="48" y="3"></use><use href="#gentle-wave" x="48" y="5"></use><use href="#gentle-wave" x="48" y="7"></use></g></svg></section><div id="post-top-cover"><img class="nolazyload" id="post-top-bg" src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png"></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="目标">目标</h1>
<ul class="lvl-0">
<li class="lvl-2">了解异常</li>
<li class="lvl-2">捕获异常</li>
<li class="lvl-2">异常的else</li>
<li class="lvl-2">异常finally</li>
<li class="lvl-2">异常的传递</li>
<li class="lvl-2">自定义异常</li>
</ul>
<h1 id="一-了解异常">一. 了解异常</h1>
<p>当检测到一个错误时,解释器就无法继续执行了,反而出现了一些错误的提示,这就是所谓的"异常"。</p>
<p>例如:以<code>r</code>方式打开一个不存在的文件。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="built_in">open</span>(<span class="string">'test.txt'</span>, <span class="string">'r'</span>)</span><br></pre></td></tr></table></figure>
<p><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Python/pythonStudy_img/image-20190305154200725.png" alt="image-2019030515420072"></p>
<h1 id="二-异常的写法">二. 异常的写法</h1>
<h2 id="2-1-语法">2.1 语法</h2>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> 可能发生错误的代码</span><br><span class="line"><span class="keyword">except</span>:</span><br><span class="line"> 如果出现异常执行的代码</span><br></pre></td></tr></table></figure>
<h2 id="2-2-快速体验">2.2 快速体验</h2>
<p>需求:尝试以<code>r</code>模式打开文件,如果文件不存在,则以<code>w</code>方式打开。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> f = <span class="built_in">open</span>(<span class="string">'test.txt'</span>, <span class="string">'r'</span>)</span><br><span class="line"><span class="keyword">except</span>:</span><br><span class="line"> f = <span class="built_in">open</span>(<span class="string">'test.txt'</span>, <span class="string">'w'</span>)</span><br></pre></td></tr></table></figure>
<h2 id="2-3-捕获指定异常(类型)">2.3 捕获指定异常(类型)</h2>
<h3 id="2-3-1-语法">2.3.1 语法</h3>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> 可能发生错误的代码</span><br><span class="line"><span class="keyword">except</span> 异常类型:</span><br><span class="line"> 如果捕获到该异常类型执行的代码</span><br></pre></td></tr></table></figure>
<h3 id="2-3-2-体验">2.3.2 体验</h3>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> <span class="built_in">print</span>(num)</span><br><span class="line"><span class="keyword">except</span> NameError:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'有错误'</span>)</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意:</p>
<ol>
<li class="lvl-3">如果尝试执行的代码的异常类型和要捕获的异常类型<font color='cornflowerblue'>不一致,则无法捕获异常。</font></li>
<li class="lvl-3"><strong>一般try下方只放一行尝试执行的代码。</strong></li>
</ol>
</blockquote>
<h3 id="2-3-3-捕获多个指定异常">2.3.3 捕获多个指定异常</h3>
<p><strong>当捕获多个异常时,可以把要捕获的异常类型的名字,放到except 后,并使用元组的方式进行书写。</strong></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="number">1</span>/<span class="number">0</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">except</span> (NameError, ZeroDivisionError):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'有错误'</span>)</span><br></pre></td></tr></table></figure>
<h3 id="2-3-4-捕获异常描述信息">2.3.4 捕获异常描述信息</h3>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> <span class="built_in">print</span>(num)</span><br><span class="line"><span class="keyword">except</span> (NameError, ZeroDivisionError) <span class="keyword">as</span> result: <span class="comment"># 捕获异常描述信息 用‘as 变量’来接收异常信息</span></span><br><span class="line"> <span class="built_in">print</span>(result)</span><br></pre></td></tr></table></figure>
<h3 id="2-3-5-捕获所有异常">2.3.5 捕获所有异常</h3>
<p>**<font color='red'>Exception是所有 程序异常类的父类。</font>**可用于捕获代码执行错误的异常类型。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> <span class="built_in">print</span>(num)</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> result: <span class="comment"># 利用Exception捕获程序异常描述信息</span></span><br><span class="line"> <span class="built_in">print</span>(result)</span><br></pre></td></tr></table></figure>
<h2 id="2-4-异常的else">2.4 异常的else</h2>
<p><strong>else表示的是如果没有异常要执行的代码。</strong></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="number">1</span>)</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> result:</span><br><span class="line"> <span class="built_in">print</span>(result)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'我是else,是没有异常的时候执行的代码'</span>)</span><br></pre></td></tr></table></figure>
<h2 id="2-5-异常的finally">2.5 异常的finally</h2>
<p><strong>finally表示的是无论是否异常都要执行的代码,例如关闭文件。</strong></p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> f = <span class="built_in">open</span>(<span class="string">'test.txt'</span>, <span class="string">'r'</span>)</span><br><span class="line"><span class="keyword">except</span> Exception <span class="keyword">as</span> result:</span><br><span class="line"> f = <span class="built_in">open</span>(<span class="string">'test.txt'</span>, <span class="string">'w'</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'没有异常,真开心'</span>)</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line"> f.close()</span><br></pre></td></tr></table></figure>
<h1 id="三-异常的传递">三. 异常的传递</h1>
<p>体验异常传递</p>
<p>需求:</p>
<p> 1. 尝试只读方式打开test.txt文件,如果文件存在则读取文件内容,文件不存在则提示用户即可。</p>
<p> 2. 读取内容要求:尝试循环读取内容,读取过程中如果检测到用户意外终止程序【去文件所在路径下执行cmd中输入<python <a target="_blank" rel="noopener external nofollow noreferrer" href="http://xn--5nqx7jp0r.py">文件名.py</a>> 执行中输入ctrl+c】,则<code>except</code>捕获异常并提示用户。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> f = <span class="built_in">open</span>(<span class="string">'test.txt'</span>)</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> <span class="keyword">while</span> <span class="literal">True</span>:</span><br><span class="line"> content = f.readline()</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">len</span>(content) == <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> time.sleep(<span class="number">2</span>)</span><br><span class="line"> <span class="built_in">print</span>(content)</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="comment"># 如果在读取文件的过程中,产生了异常,那么就会捕获到</span></span><br><span class="line"> <span class="comment"># 比如 按下了 ctrl+c</span></span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'意外终止了读取数据'</span>)</span><br><span class="line"> <span class="keyword">finally</span>:</span><br><span class="line"> f.close()</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'关闭文件'</span>)</span><br><span class="line"><span class="keyword">except</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">"没有这个文件"</span>)</span><br></pre></td></tr></table></figure>
<h1 id="四-自定义异常类">四. 自定义异常类</h1>
<p>在Python中,抛出自定义异常的语法为<code> raise 异常类对象</code>。</p>
<p><strong>作用</strong>:<font color='red'>将不满足程序逻辑要求代码时进行报错。</font></p>
<p>需求:密码长度不足,则报异常(用户输入密码,如果输入的长度不足3位,则报错,即抛出自定义异常,并捕获该异常)。</p>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 自定义异常类,继承Exception类:class Exception(object):</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ShortInputError</span>(<span class="title class_ inherited__">Exception</span>):</span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__init__</span>(<span class="params">self, length, min_len</span>):</span><br><span class="line"> self.length = length</span><br><span class="line"> self.min_len = min_len</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 设置抛出异常的描述信息</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__str__</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="keyword">return</span> <span class="string">f'你输入的长度是<span class="subst">{self.length}</span>, 不能少于<span class="subst">{self.min_len}</span>个字符'</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">main</span>():</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> con = <span class="built_in">input</span>(<span class="string">'请输入密码:'</span>)</span><br><span class="line"> <span class="keyword">if</span> <span class="built_in">len</span>(con) < <span class="number">3</span>:</span><br><span class="line"> <span class="comment"># 抛出异常类创建的对象</span></span><br><span class="line"> <span class="keyword">raise</span> ShortInputError(<span class="built_in">len</span>(con), <span class="number">3</span>) <span class="comment"># 有异常执行自定义异常类</span></span><br><span class="line"> <span class="comment"># 捕获该异常</span></span><br><span class="line"> <span class="keyword">except</span> Exception <span class="keyword">as</span> result: <span class="comment"># 多继承默认使用子类属性和方法</span></span><br><span class="line"> <span class="built_in">print</span>(result)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">'密码已经输入完成'</span>)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">main()</span><br></pre></td></tr></table></figure>
<h1 id="五-总结">五. 总结</h1>
<ul class="lvl-0">
<li class="lvl-2">
<p>异常语法</p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line"> 可能发生异常的代码</span><br><span class="line"><span class="keyword">except</span>:</span><br><span class="line"> 如果出现异常执行的代码</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> 没有异常执行的代码</span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line"> 无论是否异常都要执行的代码</span><br></pre></td></tr></table></figure>
<ul class="lvl-0">
<li class="lvl-2">
<p>捕获异常</p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="keyword">except</span> 异常类型:</span><br><span class="line"> 代码</span><br><span class="line"></span><br><span class="line"><span class="keyword">except</span> 异常类型 <span class="keyword">as</span> xx:</span><br><span class="line"> 代码</span><br></pre></td></tr></table></figure>
<ul class="lvl-0">
<li class="lvl-2">
<p>自定义异常</p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="code"><pre><span class="line"><span class="comment"># 1. 自定义异常类,继承Exception类</span></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">异常类类名</span>(<span class="title class_ inherited__">Exception</span>):</span><br><span class="line"> 代码</span><br><span class="line"> </span><br><span class="line"> <span class="comment"># 设置抛出异常的描述信息</span></span><br><span class="line"> <span class="keyword">def</span> <span class="title function_">__str__</span>(<span class="params">self</span>):</span><br><span class="line"> <span class="keyword">return</span> ...</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 抛出异常,不满足条件逻辑时可设置</span></span><br><span class="line"><span class="keyword">raise</span> 异常类名()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 捕获异常</span></span><br><span class="line"><span class="keyword">except</span> Exception...</span><br></pre></td></tr></table></figure>
</article><div class="post-tools" id="post-tools"><div class="post-tools-left"><div class="rewardLeftButton"><div class="post-reward" onclick="anzhiyu.addRewardMask()"><div class="reward-button button--animated" title="赞赏作者"><i class="anzhiyufont anzhiyu-icon-hand-heart-fill"></i>打赏作者</div><div class="reward-main"><div class="reward-all"><span class="reward-title">感谢你赐予我前进的力量</span><ul class="reward-group"><li class="reward-item"><a href="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/wx.2a62ljcc2n40.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/wx.2a62ljcc2n40.png" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/zfb.23605lp4hi1s.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/zfb.23605lp4hi1s.png" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul><a class="reward-main-btn" href="/about/#about-reward" target="_blank"><div class="reward-text">赞赏者名单</div><div class="reward-dec">因为你们的支持让我意识到写文章的价值🙏</div></a></div></div><div id="quit-box" onclick="anzhiyu.removeRewardMask()" style="display: none"></div></div></div><div class="shareRight"><div class="share-link mobile"><div class="share-qrcode"><div class="share-button" title="使用手机访问这篇文章"><i class="anzhiyufont anzhiyu-icon-qrcode"></i></div><div class="share-main"><div class="share-main-all"><div id="qrcode" title="https://www.beidaoaz.top/pythonadv_OOP_4.html"></div><div class="reward-dec">使用手机访问这篇文章</div></div></div></div></div><div class="share-link weibo"><a class="share-button" target="_blank" href="https://service.weibo.com/share/share.php?title=Python进阶(四)异常&url=https://www.beidaoaz.top/pythonadv_OOP_4.html&pic=https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" rel="external nofollow noreferrer noopener"><i class="anzhiyufont anzhiyu-icon-weibo"></i></a></div><div class="share-link copyurl"><div class="share-button" id="post-share-url" title="复制链接" onclick="rm.copyPageUrl()"><i class="anzhiyufont anzhiyu-icon-link"></i></div></div></div></div><div class="post-tools-right"><div class="tag_share"><div class="post-meta__box"><div class="post-meta__box__tag-list"><a class="post-meta__box__tags" href="tags/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/"><span class="tags-punctuation"><i class="anzhiyufont anzhiyu-icon-tag"></i></span>面向对象编程<span class="tagsPageCount">6</span></a><a class="post-meta__box__tags" href="tags/Python%E8%BF%9B%E9%98%B6/"><span class="tags-punctuation"><i class="anzhiyufont anzhiyu-icon-tag"></i></span>Python进阶<span class="tagsPageCount">6</span></a></div></div></div><div class="post_share"><div class="social-share" data-image="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Science+/read_paper/1.kupf912yxi8.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://npm.elemecdn.com/[email protected]/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"/><script src="https://npm.elemecdn.com/[email protected]/sharejs/dist/js/social-share.min.js" defer="defer"></script></div></div></div><div class="post-copyright"><i class="anzhiyufont anzhiyu-icon-copyright"></i><div class="post-copyright__author"><a class="post-copyright__original" title="该文章为原创文章,注意版权协议" href="https://www.beidaoaz.top/pythonadv_OOP_4.html">原创</a><a class="post-copyright-title"><span>Python进阶(四)异常</span></a></div><div class="post-copyright-info-box"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"></span><a class="link" href="https://www.beidaoaz.top">Beidaos</a></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a class="link" href="https://www.beidaoaz.top/pythonadv_OOP_4.html">https://www.beidaoaz.top/pythonadv_OOP_4.html</a></span><span class="copy-button" onclick="rm.copyPageUrl()"><i class="anzhiyufont anzhiyu-icon-copy"></i></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow noreferrer" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.beidaoaz.top" target="_blank">BEIDAO.</a>!</span></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="pythonadv_OOP_3.html"><img class="prev-cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Python进阶(三)面向对象—多态、类方法、类属性</div></div></a></div><div class="next-post pull-right"><a href="pythonadv_OOP_5.html"><img class="next-cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Python进阶(五)模块和包</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="anzhiyufont anzhiyu-icon-thumbs-up fa-fw" style="font-size: 1.5rem; margin-right: 4px"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="pythonadv_OOP_2.html" title="Python进阶(二)面向对象—继承"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-07-05</div><div class="title">Python进阶(二)面向对象—继承</div></div></a></div><div><a href="pythonadv_OOP_1.html" title="Python进阶(一)面向对象基础"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-07-04</div><div class="title">Python进阶(一)面向对象基础</div></div></a></div><div><a href="pythonadv_OOP_6.html" title="Python进阶(六)案例:面向对象版学员管理系统"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-07-09</div><div class="title">Python进阶(六)案例:面向对象版学员管理系统</div></div></a></div><div><a href="pythonadv_OOP_3.html" title="Python进阶(三)面向对象—多态、类方法、类属性"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-07-06</div><div class="title">Python进阶(三)面向对象—多态、类方法、类属性</div></div></a></div><div><a href="pythonadv_OOP_5.html" title="Python进阶(五)模块和包"><img class="cover" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" alt="cover"><div class="content is-center"><div class="date"><i class="anzhiyufont anzhiyu-icon-calendar-days fa-fw"></i> 2023-07-09</div><div class="title">Python进阶(五)模块和包</div></div></a></div></div></div><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="anzhiyufont anzhiyu-icon-comments"></i><span> 评论</span></div><div class="comment-randomInfo" style="display: none"><a onclick="anzhiyu.addRandomCommentInfo()" href="javascript:void(0)" rel="external nofollow noreferrer">匿名评论</a></div><div class="comment-tips" id="comment-tips"><span>你无需删除空行,直接评论以获取最佳展示效果</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div><div class="comment-barrage"></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="author-info-top"> <div class="card-info-avatar"><a class="avatar-img" href="/about"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/avatar.1tq7dqdicpmo.png" onerror="this.onerror=null;this.src='../private_ismg/404.gif'" alt="avatar"/></a></div></div><div class="author-info__sayhi" id="author-info__sayhi"></div><h1 class="author-info__name">Beidaos</h1><div class="author-info__description">Live yourself as a ray of light.</div><div class="card-info-social-icons is-center"><a class="social-icon faa-parent animated-hover" href="https://github.com/AnZhiJJ" rel="external nofollow noreferrer" target="_blank" title="Github"><i class="anzhiyufont anzhiyu-icon-github"></i></a><a class="social-icon faa-parent animated-hover" href="https://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&[email protected]" rel="external nofollow noreferrer" target="_blank" title="Email"><i class="anzhiyufont anzhiyu-icon-envelope"></i></a><a class="social-icon faa-parent animated-hover" href="https://www.ithome.com/" rel="external nofollow noreferrer" target="_blank" title="ItHome"><i class="anzhiyufont anzhiyu-icon-rss"></i></a></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-bars"></i><span>文章目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%9B%AE%E6%A0%87"><span class="toc-text">目标</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%80-%E4%BA%86%E8%A7%A3%E5%BC%82%E5%B8%B8"><span class="toc-text">一. 了解异常</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%8C-%E5%BC%82%E5%B8%B8%E7%9A%84%E5%86%99%E6%B3%95"><span class="toc-text">二. 异常的写法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E8%AF%AD%E6%B3%95"><span class="toc-text">2.1 语法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E5%BF%AB%E9%80%9F%E4%BD%93%E9%AA%8C"><span class="toc-text">2.2 快速体验</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E6%8D%95%E8%8E%B7%E6%8C%87%E5%AE%9A%E5%BC%82%E5%B8%B8%EF%BC%88%E7%B1%BB%E5%9E%8B%EF%BC%89"><span class="toc-text">2.3 捕获指定异常(类型)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-1-%E8%AF%AD%E6%B3%95"><span class="toc-text">2.3.1 语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-2-%E4%BD%93%E9%AA%8C"><span class="toc-text">2.3.2 体验</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-3-%E6%8D%95%E8%8E%B7%E5%A4%9A%E4%B8%AA%E6%8C%87%E5%AE%9A%E5%BC%82%E5%B8%B8"><span class="toc-text">2.3.3 捕获多个指定异常</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-4-%E6%8D%95%E8%8E%B7%E5%BC%82%E5%B8%B8%E6%8F%8F%E8%BF%B0%E4%BF%A1%E6%81%AF"><span class="toc-text">2.3.4 捕获异常描述信息</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-5-%E6%8D%95%E8%8E%B7%E6%89%80%E6%9C%89%E5%BC%82%E5%B8%B8"><span class="toc-text">2.3.5 捕获所有异常</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E5%BC%82%E5%B8%B8%E7%9A%84else"><span class="toc-text">2.4 异常的else</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5-%E5%BC%82%E5%B8%B8%E7%9A%84finally"><span class="toc-text">2.5 异常的finally</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%B8%89-%E5%BC%82%E5%B8%B8%E7%9A%84%E4%BC%A0%E9%80%92"><span class="toc-text">三. 异常的传递</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9B%9B-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%BC%82%E5%B8%B8%E7%B1%BB"><span class="toc-text">四. 自定义异常类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BA%94-%E6%80%BB%E7%BB%93"><span class="toc-text">五. 总结</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="" title="卷积神经网络-CNN"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Science+/read_paper/1.kupf912yxi8.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="卷积神经网络-CNN"/></a><div class="content"><a class="title" href="" title="卷积神经网络-CNN">卷积神经网络-CNN</a><time datetime="2023-07-26T00:00:00.000Z" title="发表于 2023-07-26 08:00:00">2023-07-26</time></div></div><div class="aside-list-item"><a class="thumbnail" href="deepLearning02.html" title="神经网络中的反向传播算法——BackPropagation算法"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Science+/read_paper/1.kupf912yxi8.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="神经网络中的反向传播算法——BackPropagation算法"/></a><div class="content"><a class="title" href="deepLearning02.html" title="神经网络中的反向传播算法——BackPropagation算法">神经网络中的反向传播算法——BackPropagation算法</a><time datetime="2023-07-25T10:00:00.000Z" title="发表于 2023-07-25 18:00:00">2023-07-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="deepLearning01.html" title="神经网络基础"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@Science+/read_paper/1.kupf912yxi8.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="神经网络基础"/></a><div class="content"><a class="title" href="deepLearning01.html" title="神经网络基础">神经网络基础</a><time datetime="2023-07-25T00:00:00.000Z" title="发表于 2023-07-25 08:00:00">2023-07-25</time></div></div><div class="aside-list-item"><a class="thumbnail" href="latex1.html" title="【LaTeX】新手教程:从入门到日常使用"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/image.3qlx7labk120.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="【LaTeX】新手教程:从入门到日常使用"/></a><div class="content"><a class="title" href="latex1.html" title="【LaTeX】新手教程:从入门到日常使用">【LaTeX】新手教程:从入门到日常使用</a><time datetime="2023-07-23T03:00:00.000Z" title="发表于 2023-07-23 11:00:00">2023-07-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="pythonadv_OOP_6.html" title="Python进阶(六)案例:面向对象版学员管理系统"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Python进阶(六)案例:面向对象版学员管理系统"/></a><div class="content"><a class="title" href="pythonadv_OOP_6.html" title="Python进阶(六)案例:面向对象版学员管理系统">Python进阶(六)案例:面向对象版学员管理系统</a><time datetime="2023-07-09T00:00:00.000Z" title="发表于 2023-07-09 08:00:00">2023-07-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="pythonadv_OOP_5.html" title="Python进阶(五)模块和包"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.statically.io/gh/AnZhiJJ/Blog_Img@main/博客站点配图/python-logo.6puefdh4wtk0.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Python进阶(五)模块和包"/></a><div class="content"><a class="title" href="pythonadv_OOP_5.html" title="Python进阶(五)模块和包">Python进阶(五)模块和包</a><time datetime="2023-07-09T00:00:00.000Z" title="发表于 2023-07-09 08:00:00">2023-07-09</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div id="footer_deal"><a class="deal_link" href="../mailto:[email protected]" title="email"><i class="anzhiyufont anzhiyu-icon-envelope"></i></a><a class="deal_link" target="_blank" rel="noopener external nofollow noreferrer" href="https://weibo.com/u/2792503557" title="微博"><i class="anzhiyufont anzhiyu-icon-weibo"></i></a><img class="footer_mini_logo" title="返回顶部" onclick="anzhiyu.scrollToDest(0, 500)" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/avatar.1tq7dqdicpmo.png"/><a class="deal_link" target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/AnZhiJJ" title="Github"><i class="anzhiyufont anzhiyu-icon-github"></i></a><a class="deal_link" target="_blank" rel="noopener external nofollow noreferrer" href="https://space.bilibili.com/360398463" title="Bilibili"><i class="anzhiyufont anzhiyu-icon-bilibili"></i></a></div></div><div id="ah-footer"><div class="footer-group"><h3 class="footer-title">博客文章</h3><div class="footer-links"><a class="footer-item" href="/archives/" data-pjax-state="">全部文章</a><a class="footer-item" href="/tags/" data-pjax-state="">文章标签</a><a class="footer-item" href="/categories/" data-pjax-state="">文章分类</a></div></div><div class="footer-group"><h3 class="footer-title">学习笔记</h3><div class="footer-links"><a class="footer-item" href="/categories/C语法/" data-pjax-state="">C 语法</a><a class="footer-item" href="/categories/Python语法/" data-pjax-state="">Python语法</a><a class="footer-item" href="/categories/技术文档/" data-pjax-state="">技术文档</a></div></div><div class="footer-group"><h3 class="footer-title">博客链接</h3><div class="footer-links"><a class="footer-item" href="/album/" data-pjax-state="">相册集</a><a class="footer-item" href="/collect/" data-pjax-state="">藏宝阁</a><a class="footer-item" href="/essay/" data-pjax-state="">闲言碎语</a></div></div><div class="footer-group"><h3 class="footer-title">关于本站</h3><div class="footer-links"><a class="footer-item" href="/about/" data-pjax-state="">关于博主</a><a class="footer-item" href="/cc/" data-pjax-state="">版权协议</a></div></div></div><div id="fotter-copyright" style="font-size:100%"><div class="copyrights"><span>Copyright © 2022 Beidaos. </span><span> All rights reserved.</span></div><div class="framework-info"><span>Powered by </span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://hexo.io">Hexo</a><span class="footer-separator">&</span><a target="_blank" rel="noopener external nofollow noreferrer" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/avatar.1tq7dqdicpmo.png" onerror="onerror=null;src='/private_ismg/404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="archives/" title="archive"><div class="headline">文章</div><div class="length-num">32</div></a><a href="tags/" title="tag"><div class="headline">标签</div><div class="length-num">16</div></a><a href="categories/" title="category"><div class="headline">分类</div><div class="length-num">8</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 文章</span></a><ul class="menus_item_child" style="left:-79px;"><li><a class="site-page child faa-parent animated-hover" href="/archives/"><i class="anzhiyufont anzhiyu-icon-box-archive faa-tada" style="font-size: 0.9em;"></i><span> 归档</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/categories/"><i class="anzhiyufont anzhiyu-icon-shapes faa-tada" style="font-size: 0.9em;"></i><span> 分类</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/tags/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-biaoqiantags3"></use></svg><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 社交</span></a><ul class="menus_item_child" style="left:-31px;"><li><a class="site-page child faa-parent animated-hover" href="/link/"><i class="anzhiyufont anzhiyu-icon-link faa-tada" style="font-size: 0.9em;"></i><span> 友链</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/essay/"><i class="anzhiyufont anzhiyu-icon-lightbulb faa-tada" style="font-size: 0.9em;"></i><span> 闲言碎语</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 我的</span></a><ul class="menus_item_child" style="left:-79px;"><li><a class="site-page child faa-parent animated-hover" href="/music/?id=886726002&server=netease"><i class="anzhiyufont anzhiyu-icon-music faa-tada" style="font-size: 0.9em;"></i><span> 音乐馆</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/collect/"><svg class="icon faa-tada" aria-hidden="true"><use xlink:href="#icon-dropboxdropbox4"></use></svg><span> 藏宝阁</span></a></li><li><a class="site-page child faa-parent animated-hover" href="/album/"><i class="anzhiyufont anzhiyu-icon-images faa-tada" style="font-size: 0.9em;"></i><span> 相册集</span></a></li></ul></div><div class="menus_item"><a class="site-page faa-parent animated-hover" href="javascript:void(0);" rel="external nofollow noreferrer"><span> 关于</span></a><ul class="menus_item_child" style="left:17px;"><li><a class="site-page child faa-parent animated-hover" href="/about/"><i class="anzhiyufont anzhiyu-icon-paper-plane faa-tada" style="font-size: 0.9em;"></i><span> 关于博主</span></a></li></ul></div></div></div></div><div id="keyboard-tips"><div class="keyboardTitle">博客快捷键</div><div class="keybordList"><div class="keybordItem"><div class="keyGroup"><div class="key">shift K</div></div><div class="keyContent"><div class="content">关闭快捷键功能</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift A</div></div><div class="keyContent"><div class="content">打开中控台</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift M</div></div><div class="keyContent"><div class="content">播放/暂停音乐</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift D</div></div><div class="keyContent"><div class="content">深色/浅色显示模式</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift S</div></div><div class="keyContent"><div class="content">站内搜索</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift R</div></div><div class="keyContent"><div class="content">随机访问</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift H</div></div><div class="keyContent"><div class="content">返回首页</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift F</div></div><div class="keyContent"><div class="content">友链鱼塘</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift L</div></div><div class="keyContent"><div class="content">友链页面</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift P</div></div><div class="keyContent"><div class="content">关于本站</div></div></div><div class="keybordItem"><div class="keyGroup"><div class="key">shift I</div></div><div class="keyContent"><div class="content">原版右键菜单</div></div></div></div></div><script>var anzhiyu_keyboard = false
var anzhiyu_intype = false
var anzhiyu_keyUpEvent_timeoutId = null
function addKeyShotListener() {
const windowObject = window;
windowObject.removeEventListener("keydown", keyDownEvent);
windowObject.removeEventListener("keyup", keyUpEvent);
windowObject.addEventListener("keydown", keyDownEvent);
windowObject.addEventListener("keyup", keyUpEvent);
}
function keyDownEvent(event) {
const isEscapeKeyPressed = event.keyCode === 27;
const isShiftKeyPressed = event.shiftKey;
const isKeyboardEnabled = anzhiyu_keyboard;
const isInInputField = anzhiyu_intype;
if (isEscapeKeyPressed) {
anzhiyu.hideLoading();
anzhiyu.hideConsole();
rm.hideRightMenu();
}
if (isKeyboardEnabled && isShiftKeyPressed && !isInInputField) {
switch (event.keyCode) {
case 16:
anzhiyu_keyUpEvent_timeoutId = setTimeout(()=>{
document.querySelector("#keyboard-tips").classList.add("show");
}, 200);
break;
case 65:
anzhiyu.showConsole();
break;
case 77:
anzhiyu.musicToggle();
case 75:
anzhiyu.keyboardToggle();
case 73:
anzhiyu.rightMenuToggle();
break;
case 82:
toRandomPost();
break;
case 72:
pjax.loadUrl("/");
break;
case 68:
rm.switchDarkMode();
break;
case 70:
pjax.loadUrl("/fcircle/");
break;
case 76:
pjax.loadUrl("/link/");
break;
case 80:
pjax.loadUrl("/about/");
break;
default:
break;
}
event.preventDefault();
}
}
addKeyShotListener();
window.onfocus = function() {
document.getElementById("keyboard-tips").classList.remove("show")
}
function setInputFocusListener() {
const inputs = document.querySelectorAll("input, textarea");
inputs.forEach((input) => {
input.addEventListener("focus", () => {
setAnzhiyuIntype(true);
});
input.addEventListener("blur", () => {
setAnzhiyuIntype(false);
});
});
}
function setanzhiyuIntype(value) {
anzhiyu_intype = value;
}
function keyUpEvent(event) {
anzhiyu_keyUpEvent_timeoutId && clearTimeout(anzhiyu_keyUpEvent_timeoutId);
if (event.keyCode === 16) {
const keyboardTips = document.querySelector("#keyboard-tips");
keyboardTips.classList.remove("show");
}
}
function listenToPageInputPress() {
const inputElement = document.getElementById("toPageText");
const buttonElement = document.getElementById("toPageButton");
if (!inputElement) {
return;
}
inputElement.addEventListener("keydown", (event) => {
if (event.keyCode === 13) {
event.preventDefault();
anzhiyu.toPage();
pjax.loadUrl(buttonElement.href);
}
});
inputElement.addEventListener("input", () => {
const value = inputElement.value;
if (!value || value === "0") {
buttonElement.classList.remove("haveValue");
} else {
buttonElement.classList.add("haveValue");
}
const pageNumbers = document.querySelectorAll(".page-number");
const maxPageNumber = +pageNumbers[pageNumbers.length - 1].innerHTML;
if (+value > maxPageNumber) {
inputElement.value = maxPageNumber;
}
});
}
setInputFocusListener()</script><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="anzhiyufont anzhiyu-icon-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button type="button" title="切换背景" onclick="toggleWinbox()"><i class="iconfont icon-qiehuankapian"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="anzhiyufont anzhiyu-icon-arrows-left-right"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="anzhiyufont anzhiyu-icon-gear"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="anzhiyufont anzhiyu-icon-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="anzhiyufont anzhiyu-icon-comments"></i></a><a id="switch_commentBarrage" href="javascript:anzhiyu.switchCommentBarrage();" rel="external nofollow noreferrer" title="开关弹幕"><i class="anzhiyufont anzhiyu-icon-danmu"></i></a><button id="center-console" type="button" title="中控台"><i class="iconfont icon-kongzhitai"></i></button><button id="go-up" type="button" title="回到顶部"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></button></div></div><div id="nav-music"><div id="nav-music-hoverTips" onclick="anzhiyu.musicToggle()">播放音乐</div><div id="console-music-bg"></div><meting-js id="886726002" server="netease" type="playlist" mutex="true" preload="none" theme="var(--anzhiyu-main)" data-lrctype="0" order="random"></meting-js></div><div id="console"><div class="close-btn" onclick="anzhiyu.hideConsole()" href="javascript:void(0);"><i class="anzhiyufont anzhiyu-icon-circle-xmark" style="font-size: 35px;"></i></div><div class="console-card-group-reward"><ul class="reward-all console-card"><li class="reward-item"><a href="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/wx.2a62ljcc2n40.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" alt="wechat" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/wx.2a62ljcc2n40.png"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/zfb.23605lp4hi1s.png" rel="external nofollow noreferrer" target="_blank"><img class="post-qr-code-img" alt="alipay" src= "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" data-lazy-src="https://cdn.staticaly.com/gh/AnZhiJJ/Blog_Img@main/博客站点配图/zfb.23605lp4hi1s.png"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div><div class="console-card-group"><div class="console-card-group-left"><div class="console-card" id="card-newest-comments" onclick="anzhiyu.hideConsole()"><div class="card-content"><div class="author-content-item-tips">互动</div><span class="author-content-item-title"> <span>最新评论</span></span></div><div class="aside-list"><span>正在加载中...</span></div></div></div><div class="console-card-group-right"><div class="console-card tags" onclick="anzhiyu.hideConsole()"><div class="card-content"><div class="author-content-item-tips">兴趣点</div><span class="author-content-item-title">寻找你感兴趣的领域</span><div class="card-tags"><div class="item-headline"></div><div class="card-tag-cloud"><a href="tags/BP%E7%AE%97%E6%B3%95/" style="font-size: 1.05rem;">BP算法<sup>1</sup></a><a href="tags/Catia/" style="font-size: 1.05rem;">Catia<sup>1</sup></a><a href="tags/C%E8%AF%AD%E6%B3%95/" style="font-size: 1.05rem; font-weight: 500; color: var(--anzhiyu-lighttext)">C语法<sup>2</sup></a><a href="tags/DNS/" style="font-size: 1.05rem;">DNS<sup>1</sup></a><a href="tags/LaTeX/" style="font-size: 1.05rem;">LaTeX<sup>1</sup></a><a href="tags/Python%E8%AF%AD%E6%B3%95/" style="font-size: 1.05rem; font-weight: 500; color: var(--anzhiyu-lighttext)">Python语法<sup>16</sup></a><a href="tags/Python%E8%BF%9B%E9%98%B6/" style="font-size: 1.05rem;">Python进阶<sup>6</sup></a><a href="tags/conda/" style="font-size: 1.05rem;">conda<sup>1</sup></a><a href="tags/pip/" style="font-size: 1.05rem;">pip<sup>1</sup></a><a href="tags/%E5%8D%B7%E7%A7%AF%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" style="font-size: 1.05rem;">卷积神经网络<sup>1</sup></a><a href="tags/%E5%9F%9F%E5%90%8D%E8%A7%A3%E6%9E%90/" style="font-size: 1.05rem;">域名解析<sup>1</sup></a><a href="tags/%E6%8C%87%E9%92%88/" style="font-size: 1.05rem;">指针<sup>1</sup></a><a href="tags/%E6%96%87%E7%8C%AE%E7%AC%94%E8%AE%B0/" style="font-size: 1.05rem;">文献笔记<sup>1</sup></a><a href="tags/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/" style="font-size: 1.05rem;">深度学习<sup>3</sup></a><a href="tags/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C/" style="font-size: 1.05rem;">神经网络<sup>1</sup></a><a href="tags/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" style="font-size: 1.05rem;">面向对象编程<sup>6</sup></a></div></div><hr/></div></div><div class="console-card history" onclick="anzhiyu.hideConsole()"><div class="item-headline"><i class="anzhiyufont anzhiyu-icon-box-archiv"></i><span>文章</span></div></div></div></div><div class="button-group"><div class="console-btn-item"><a class="darkmode_switchbutton" onclick="rm.switchDarkMode()" title="显示模式切换" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-moon"></i></a></div><div class="console-btn-item" id="consoleHideAside" onclick="anzhiyu.hideAsideBtn()" title="边栏显示控制"><a class="asideSwitch"><i class="anzhiyufont anzhiyu-icon-arrows-left-right"></i></a></div><div class="console-btn-item on" id="consoleCommentBarrage" onclick="anzhiyu.switchCommentBarrage()" title="热评开关"><a class="commentBarrage"><i class="anzhiyufont anzhiyu-icon-message"></i></a></div><div class="console-btn-item" id="consoleMusic" onclick="anzhiyu.musicToggle()" title="音乐开关"><a class="music-switch"><i class="anzhiyufont anzhiyu-icon-music"></i></a></div><div class="console-btn-item" id="consoleKeyboard" onclick="anzhiyu.keyboardToggle()" title="快捷键开关"><a class="keyboard-switch"><i class="anzhiyufont anzhiyu-icon-keyboard"></i></a></div></div><div class="console-mask" onclick="anzhiyu.hideConsole()" href="javascript:void(0);" rel="external nofollow noreferrer"></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="anzhiyufont anzhiyu-icon-xmark"></i></button></nav><div class="is-center" id="loading-database"><i class="anzhiyufont anzhiyu-icon-spinner anzhiyu-pulse-icon"></i><span> 数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div id="rightMenu"><div class="rightMenu-group rightMenu-small"><div class="rightMenu-item" id="menu-backward"><i class="anzhiyufont anzhiyu-icon-arrow-left"></i></div><div class="rightMenu-item" id="menu-forward"><i class="anzhiyufont anzhiyu-icon-arrow-right"></i></div><div class="rightMenu-item" id="menu-refresh"><i class="anzhiyufont anzhiyu-icon-arrow-rotate-right" style="font-size: 1rem;"></i></div><div class="rightMenu-item" id="menu-top"><i class="anzhiyufont anzhiyu-icon-arrow-up"></i></div></div><div class="rightMenu-group rightMenu-line rightMenuPlugin"><div class="rightMenu-item" id="menu-copytext"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制选中文本</span></div><div class="rightMenu-item" id="menu-pastetext"><i class="anzhiyufont anzhiyu-icon-paste"></i><span>粘贴文本</span></div><a class="rightMenu-item" id="menu-commenttext"><i class="anzhiyufont anzhiyu-icon-comment-medical"></i><span>引用到评论</span></a><div class="rightMenu-item" id="menu-newwindow"><i class="anzhiyufont anzhiyu-icon-window-restore"></i><span>新窗口打开</span></div><div class="rightMenu-item" id="menu-copylink"><i class="anzhiyufont anzhiyu-icon-link"></i><span>复制链接地址</span></div><div class="rightMenu-item" id="menu-copyimg"><i class="anzhiyufont anzhiyu-icon-images"></i><span>复制此图片</span></div><div class="rightMenu-item" id="menu-downloadimg"><i class="anzhiyufont anzhiyu-icon-download"></i><span>下载此图片</span></div><div class="rightMenu-item" id="menu-newwindowimg"><i class="anzhiyufont anzhiyu-icon-window-restore"></i><span>新窗口打开图片</span></div><div class="rightMenu-item" id="menu-search"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span>站内搜索</span></div><div class="rightMenu-item" id="menu-searchBaidu"><i class="anzhiyufont anzhiyu-icon-magnifying-glass"></i><span>百度搜索</span></div><div class="rightMenu-item" id="menu-music-toggle"><i class="anzhiyufont anzhiyu-icon-play"></i><span>播放音乐</span></div><div class="rightMenu-item" id="menu-music-back"><i class="anzhiyufont anzhiyu-icon-backward"></i><span>切换到上一首</span></div><div class="rightMenu-item" id="menu-music-forward"><i class="anzhiyufont anzhiyu-icon-forward"></i><span>切换到下一首</span></div><div class="rightMenu-item" id="menu-music-playlist" onclick="window.open("https://music.163.com/#/my/m/music/playlist?id=886726002", "_blank");" style="display: none;"><i class="anzhiyufont anzhiyu-icon-radio"></i><span>查看所有歌曲</span></div><div class="rightMenu-item" id="menu-music-copyMusicName"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制歌名</span></div></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item menu-link" id="menu-randomPost"><i class="anzhiyufont anzhiyu-icon-shuffle"></i><span>随便逛逛</span></a><a class="rightMenu-item menu-link" href="/categories/"><i class="anzhiyufont anzhiyu-icon-cube"></i><span>博客分类</span></a><a class="rightMenu-item menu-link" href="/tags/"><i class="anzhiyufont anzhiyu-icon-tags"></i><span>文章标签</span></a></div><div class="rightMenu-group rightMenu-line rightMenuOther"><a class="rightMenu-item" id="menu-copy" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-copy"></i><span>复制地址</span></a><a class="rightMenu-item" id="menu-commentBarrage" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-message"></i><span class="menu-commentBarrage-text">关闭热评</span></a><a class="rightMenu-item" id="menu-darkmode" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-circle-half-stroke"></i><span class="menu-darkmode-text">深色模式</span></a><a class="rightMenu-item" id="menu-translate" href="javascript:void(0);" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-language"></i><span>轉為繁體</span></a></div></div><div id="rightmenu-mask"></div><div><script src="https://npm.elemecdn.com/[email protected]/source/js/utils.js"></script><script src="https://npm.elemecdn.com/[email protected]/source/js/main.js"></script><script src="https://npm.elemecdn.com/[email protected]/source/js/tw_cn.js"></script><script src="https://npm.elemecdn.com/@fancyapps/[email protected]/dist/fancybox.umd.js"></script><script src="https://npm.elemecdn.com/[email protected]/instantpage.js" type="module"></script><script src="https://npm.elemecdn.com/[email protected]/dist/lazyload.iife.min.js"></script><script src="https://npm.elemecdn.com/[email protected]/dist/snackbar.min.js"></script><canvas id="universe"></canvas><script async src="https://npm.elemecdn.com/[email protected]/dark/dark.js"></script><script>// 消除控制台打印
var HoldLog = console.log;
console.log = function () {};
let now1 = new Date();
queueMicrotask(() => {
const Log = function () {
HoldLog.apply(console, arguments);
}; //在恢复前输出日志
const grt = new Date("04/01/2021 00:00:00"); //此处修改你的建站时间或者网站上线时间
now1.setTime(now1.getTime() + 250);
const days = (now1 - grt) / 1000 / 60 / 60 / 24;
const dnum = Math.floor(days);
const ascll = [
`欢迎使用安知鱼!`,
`生活明朗, 万物可爱`,
`
█████╗ ███╗ ██╗███████╗██╗ ██╗██╗██╗ ██╗██╗ ██╗
██╔══██╗████╗ ██║╚══███╔╝██║ ██║██║╚██╗ ██╔╝██║ ██║
███████║██╔██╗ ██║ ███╔╝ ███████║██║ ╚████╔╝ ██║ ██║
██╔══██║██║╚██╗██║ ███╔╝ ██╔══██║██║ ╚██╔╝ ██║ ██║
██║ ██║██║ ╚████║███████╗██║ ██║██║ ██║ ╚██████╔╝
╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝
`,
"已上线",
dnum,
"天",
"©2022 By 安知鱼",
];
const ascll2 = [`NCC2-036`, `调用前置摄像头拍照成功,识别为【小笨蛋】.`, `Photo captured: `, `🤪`];
setTimeout(
Log.bind(
console,
`\n%c${ascll[0]} %c ${ascll[1]} %c ${ascll[2]} %c${ascll[3]}%c ${ascll[4]}%c ${ascll[5]}\n\n%c ${ascll[6]}\n`,
"color:#3b70fc",
"",
"color:#3b70fc",
"color:#3b70fc",
"",
"color:#3b70fc",
""
)
);
setTimeout(
Log.bind(
console,
`%c ${ascll2[0]} %c ${ascll2[1]} %c \n${ascll2[2]} %c\n${ascll2[3]}\n`,
"color:white; background-color:#4fd953",
"",
"",
'background:url("https://npm.elemecdn.com/[email protected]/img/post/common/tinggge.gif") no-repeat;font-size:450%'
)
);
setTimeout(Log.bind(console, "%c WELCOME %c 你好,小笨蛋.", "color:white; background-color:#4f90d9", ""));
setTimeout(
console.warn.bind(
console,
"%c ⚡ Powered by 安知鱼 %c 你正在访问 Beidaos 的博客.",
"color:white; background-color:#f0ad4e",
""
)
);
setTimeout(Log.bind(console, "%c W23-12 %c 你已打开控制台.", "color:white; background-color:#4f90d9", ""));
setTimeout(
console.warn.bind(console, "%c S013-782 %c 你现在正处于监控中.", "color:white; background-color:#d9534f", "")
);
});</script><script async src="/anzhiyu/random.js"></script><script src="https://npm.elemecdn.com/[email protected]/source/js/search/local-search.js"></script><div class="js-pjax"><script>(()=>{
const init = () => {
twikoo.init(Object.assign({
el: '#twikoo-wrap',
envId: 'https://twikoo.beidaoaz.top/',
region: '',
onCommentLoaded: function () {
anzhiyu.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.tk-owo-emotion)'))
}
}, null))
}
const getCount = () => {
const countELement = document.getElementById('twikoo-count')
if(!countELement) return
twikoo.getCommentsCount({
envId: 'https://twikoo.beidaoaz.top/',
region: '',
urls: [window.location.pathname],
includeReply: false
}).then(function (res) {
countELement.innerText = res[0].count
}).catch(function (err) {
console.error(err);
});
}
const runFn = () => {
init()
}
const loadTwikoo = () => {
if (typeof twikoo === 'object') {
setTimeout(runFn,0)
return
}
getScript('https://npm.elemecdn.com/[email protected]/dist/twikoo.all.min.js').then(runFn)
}
if ('Twikoo' === 'Twikoo' || !false) {
if (false) anzhiyu.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
else loadTwikoo()
} else {
window.loadOtherComment = () => {
loadTwikoo()
}
}
})()</script><input type="hidden" name="page-type" id="page-type" value="post"></div><script>window.addEventListener('load', () => {
const changeContent = (content) => {
if (content === '') return content
content = content.replace(/<img.*?src="(.*?)"?[^\>]+>/ig, '[图片]') // replace image link
content = content.replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[链接]') // replace url
content = content.replace(/<pre><code>.*?<\/pre>/gi, '[代码]') // replace code
content = content.replace(/<[^>]+>/g,"") // remove html tag
if (content.length > 150) {
content = content.substring(0,150) + '...'
}
return content
}
const getComment = () => {
const runTwikoo = () => {
twikoo.getRecentComments({
envId: 'https://twikoo.beidaoaz.top/',
region: '',
pageSize: 6,
includeReply: true
}).then(function (res) {
const twikooArray = res.map(e => {
return {
'content': changeContent(e.comment),
'avatar': e.avatar,
'nick': e.nick,
'url': e.url + '#' + e.id,
'date': new Date(e.created).toISOString()
}
})
saveToLocal.set('twikoo-newest-comments', JSON.stringify(twikooArray), 10/(60*24))
generateHtml(twikooArray)
}).catch(function (err) {
const $dom = document.querySelector('#card-newest-comments .aside-list')
$dom.innerHTML= "无法获取评论,请确认相关配置是否正确"
})
}
if (typeof twikoo === 'object') {
runTwikoo()
} else {
getScript('https://npm.elemecdn.com/[email protected]/dist/twikoo.all.min.js').then(runTwikoo)
}
}
const generateHtml = array => {
let result = ''
if (array.length) {
for (let i = 0; i < array.length; i++) {
result += '<div class=\'aside-list-item\'>'
if (true) {
const name = 'data-lazy-src'
result += `<a href='${array[i].url}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'><div class='name'><span>${array[i].nick}</span></div></a>`
}
result += `<div class='content'>
<a class='comment' href='${array[i].url}' title='${array[i].content}'>${array[i].content}</a>
<time datetime="${array[i].date}">${anzhiyu.diffDate(array[i].date, true)}</time></div>
</div>`
}
} else {
result += '没有评论'
}
let $dom = document.querySelector('#card-newest-comments .aside-list')
$dom.innerHTML= result
window.lazyLoadInstance && window.lazyLoadInstance.update()
window.pjax && window.pjax.refresh($dom)
}
const newestCommentInit = () => {
if (document.querySelector('#card-newest-comments .aside-list')) {
const data = saveToLocal.get('twikoo-newest-comments')
if (data) {
generateHtml(JSON.parse(data))
} else {
getComment()
}
}
}
newestCommentInit()
document.addEventListener('pjax:complete', newestCommentInit)
})</script><script async data-pjax src="https://npm.elemecdn.com/[email protected]/bubble/bubble.js"></script><script data-pjax="true">if (document.querySelector(".comment-barrage")){
var commentBarrageConfig = {
maxBarrage: 1,
barrageTime: 4000,
twikooUrl: "https://twikoo.beidaoaz.top/",
accessToken: "",
pageUrl: window.location.pathname,
barrageTimer: [],
barrageList: [],
barrageIndex: 0,
dom: document.querySelector(".comment-barrage"),
};
var commentInterval = null;
var hoverOnCommentBarrage = false;
document.querySelector(".comment-barrage").addEventListener("mouseenter", function() {
hoverOnCommentBarrage = true;
console.log("热评悬浮");
});
document.querySelector(".comment-barrage").addEventListener("mouseleave", function() {
hoverOnCommentBarrage = false;
console.log("停止悬浮");
});
function initCommentBarrage() {
if (!commentBarrageConfig.dom) return;
var data = JSON.stringify({
event: "COMMENT_GET",
"commentBarrageConfig.accessToken": commentBarrageConfig.accessToken,
url: commentBarrageConfig.pageUrl,
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4 && this.responseText) {
commentBarrageConfig.barrageList = commentLinkFilter(JSON.parse(this.responseText).data);
commentBarrageConfig.dom.innerHTML = "";
}
});
xhr.open("POST", commentBarrageConfig.twikooUrl);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(data);
clearInterval(commentInterval);
commentInterval = null;
commentInterval = setInterval(() => {
if (commentBarrageConfig.barrageList.length && !hoverOnCommentBarrage) {
popCommentBarrage(commentBarrageConfig.barrageList[commentBarrageConfig.barrageIndex]);
commentBarrageConfig.barrageIndex += 1;
commentBarrageConfig.barrageIndex %= commentBarrageConfig.barrageList.length;
}
if (
commentBarrageConfig.barrageTimer.length >
(commentBarrageConfig.barrageList.length > commentBarrageConfig.maxBarrage
? commentBarrageConfig.maxBarrage
: commentBarrageConfig.barrageList.length) &&
!hoverOnCommentBarrage
) {
removeCommentBarrage(commentBarrageConfig.barrageTimer.shift());
}
}, commentBarrageConfig.barrageTime);
}
function commentLinkFilter(data) {
data.sort((a, b) => {
return a.created - b.created;
});
let newData = [];
data.forEach(item => {
newData.push(...getCommentReplies(item));
});
return newData;
}
function getCommentReplies(item) {
if (item.replies) {
let replies = [item];
item.replies.forEach(item => {
replies.push(...getCommentReplies(item));
});
return replies;
} else {
return [];
}
}
function popCommentBarrage(data) {
let barrage = document.createElement("div");
barrage.className = "comment-barrage-item";
barrage.innerHTML = `
<div class="barrageHead">
<a class="barrageTitle ${
data.mailMd5 === "d338f432ad0bf2f61e5fe4ad1642725d" ? "barrageBloggerTitle" : ""
}" href="javascript:anzhiyu.scrollTo('#post-comment')"">
${data.mailMd5 === "d338f432ad0bf2f61e5fe4ad1642725d" ? "博主" : "热评"}
</a>
<div class="barrageNick">${data.nick}</div>
<img class="nolazyload barrageAvatar" src="https://cravatar.cn/avatar/${data.mailMd5}"/>
<a class="comment-barrage-close" href="javascript:anzhiyu.switchCommentBarrage()" rel="external nofollow noreferrer"><i class="anzhiyufont anzhiyu-icon-xmark"></i></a>
</div>
<a class="barrageContent" href="#${data.id}">
<object>${data.comment}</object>
</a>
`;
commentBarrageConfig.barrageTimer.push(barrage);
commentBarrageConfig.dom.append(barrage);
}
function removeCommentBarrage(barrage) {
barrage.className = "comment-barrage-item out";
setTimeout(() => {
if (commentBarrageConfig.dom && commentBarrageConfig.dom.contains(barrage)) {
commentBarrageConfig.dom.removeChild(barrage);
}
}, 1000);
}
// 自动隐藏
const commentEntryCallback = (entries) => {
const commentBarrage = document.querySelector(".comment-barrage");
const postComment = document.getElementById("post-comment");
entries.forEach(entry => {
if (postComment && commentBarrage && document.body.clientWidth > 768) {
commentBarrage.style.bottom = entry.isIntersecting ? "-200px" : "0";
}
});
};
// 创建IntersectionObserver实例
const observer = new IntersectionObserver(commentEntryCallback, {
root: null,
rootMargin: "0px",
threshold: 0
});
// 监视目标元素
const postCommentTarget = document.getElementById("post-comment");
if (postCommentTarget) {
observer.observe(postCommentTarget);
}
initCommentBarrage();
if (localStorage.getItem("commentBarrageSwitch") !== "false") {
document.querySelector(".comment-barrage").style.display = "flex";
document.querySelector(".menu-commentBarrage-text").textContent = "关闭热评";
} else {
document.querySelector(".comment-barrage").style.display = "none";
document.querySelector(".menu-commentBarrage-text").textContent = "显示热评";
}
document.addEventListener("pjax:send", function () {
clearInterval(commentInterval);
});
}</script><script data-pjax src="https://npm.elemecdn.com/[email protected]/catalog-bar/catalog-bar.js"></script><script async data-pjax src="https://npm.elemecdn.com/[email protected]/categoryBar/categoryBar.js"></script><script async data-pjax src="https://npm.elemecdn.com/[email protected]/waterfall/waterfall.js"></script><script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/qrcodejs/1.0.0/qrcode.min.js"></script><script>// 初始化函数
let rm = {};
//禁止图片拖拽
let imgElements = document.getElementsByTagName("img");
for (let i = 0; i < imgElements.length; i++) {
imgElements[i].addEventListener("dragstart", function (event) {
event.preventDefault();
});
}
// 显示菜单
rm.showRightMenu = function (isTrue, x = 0, y = 0) {
console.info(x, y)
let rightMenu = document.getElementById("rightMenu");
rightMenu.style.top = x + "px";
rightMenu.style.left = y + "px";
if (isTrue) {
rightMenu.style.display = "block";
stopMaskScroll();
} else {
rightMenu.style.display = "none";
}
};
// 隐藏菜单
rm.hideRightMenu = function () {
rm.showRightMenu(false);
let rightMenuMask = document.querySelector("#rightmenu-mask");
rightMenuMask.style.display = "none";
};
// 尺寸
let rmWidth = document.getElementById("rightMenu").offsetWidth;
let rmHeight = document.getElementById("rightMenu").offsetHeight;
// 重新定义尺寸
rm.reloadrmSize = function () {
rightMenu.style.visibility = "hidden";
rightMenu.style.display = "block";
// 获取宽度和高度
rmWidth = document.getElementById("rightMenu").offsetWidth;
rmHeight = document.getElementById("rightMenu").offsetHeight;
rightMenu.style.visibility = "visible";
};
// 获取点击的href
let domhref = "";
let domImgSrc = "";
let globalEvent = null;
var oncontextmenuFunction = function (event) {
if (document.body.clientWidth > 768) {
let pageX = event.clientX + 10; //加10是为了防止显示时鼠标遮在菜单上
let pageY = event.clientY;
//其他额外菜单
const $rightMenuOther = document.querySelector(".rightMenuOther");
const $rightMenuPlugin = document.querySelector(".rightMenuPlugin");
const $rightMenuCopyText = document.querySelector("#menu-copytext");
const $rightMenuPasteText = document.querySelector("#menu-pastetext");
const $rightMenuCommentText = document.querySelector("#menu-commenttext");
const $rightMenuNewWindow = document.querySelector("#menu-newwindow");
const $rightMenuNewWindowImg = document.querySelector("#menu-newwindowimg");
const $rightMenuCopyLink = document.querySelector("#menu-copylink");
const $rightMenuCopyImg = document.querySelector("#menu-copyimg");
const $rightMenuDownloadImg = document.querySelector("#menu-downloadimg");
const $rightMenuSearch = document.querySelector("#menu-search");
const $rightMenuSearchBaidu = document.querySelector("#menu-searchBaidu");
const $rightMenuMusicToggle = document.querySelector("#menu-music-toggle");
const $rightMenuMusicBack = document.querySelector("#menu-music-back");
const $rightMenuMusicForward = document.querySelector("#menu-music-forward");
const $rightMenuMusicPlaylist = document.querySelector("#menu-music-playlist");
const $rightMenuMusicCopyMusicName = document.querySelector("#menu-music-copyMusicName");
let href = event.target.href;
let imgsrc = event.target.currentSrc;
// 判断模式 扩展模式为有事件
let pluginMode = false;
$rightMenuOther.style.display = "block";
globalEvent = event;
// 检查是否需要复制 是否有选中文本
if (selectTextNow && window.getSelection()) {
pluginMode = true;
$rightMenuCopyText.style.display = "block";
$rightMenuCommentText.style.display = "block";
$rightMenuSearch.style.display = "block";
$rightMenuSearchBaidu.style.display = "block";
} else {
$rightMenuCopyText.style.display = "none";
$rightMenuCommentText.style.display = "none";
$rightMenuSearchBaidu.style.display = "none";
$rightMenuSearch.style.display = "none";
}
//检查是否右键点击了链接a标签
if (href) {
pluginMode = true;
$rightMenuNewWindow.style.display = "block";
$rightMenuCopyLink.style.display = "block";
domhref = href;
} else {
$rightMenuNewWindow.style.display = "none";
$rightMenuCopyLink.style.display = "none";
}
//检查是否需要复制图片
if (imgsrc) {
pluginMode = true;
$rightMenuCopyImg.style.display = "block";
$rightMenuDownloadImg.style.display = "block";
$rightMenuNewWindowImg.style.display = "block";
document.getElementById("rightMenu").style.width="12rem"
domImgSrc = imgsrc;
} else {
$rightMenuCopyImg.style.display = "none";
$rightMenuDownloadImg.style.display = "none";
$rightMenuNewWindowImg.style.display = "none";
}
// 判断是否为输入框
if (event.target.tagName.toLowerCase() === "input" || event.target.tagName.toLowerCase() === "textarea") {
pluginMode = true;
$rightMenuPasteText.style.display = "block";
} else {
$rightMenuPasteText.style.display = "none";
}
const navMusicEl = document.querySelector("#nav-music");
//判断是否是音乐
if (navMusicEl && navMusicEl.contains(event.target)) {
pluginMode = true;
$rightMenuMusicToggle.style.display = "block";
$rightMenuMusicBack.style.display = "block";
$rightMenuMusicForward.style.display = "block";
$rightMenuMusicPlaylist.style.display = "block";
$rightMenuMusicCopyMusicName.style.display = "block";
} else {
$rightMenuMusicToggle.style.display = "none";
$rightMenuMusicBack.style.display = "none";
$rightMenuMusicForward.style.display = "none";
$rightMenuMusicPlaylist.style.display = "none";
$rightMenuMusicCopyMusicName.style.display = "none";
}
// 如果不是扩展模式则隐藏扩展模块
if (pluginMode) {
$rightMenuOther.style.display = "none";
$rightMenuPlugin.style.display = "block";
} else {
$rightMenuPlugin.style.display = "none";
}
rm.reloadrmSize();
// 鼠标默认显示在鼠标右下方,当鼠标靠右或靠下时,将菜单显示在鼠标左方\上方
if (pageX + rmWidth > window.innerWidth) {
pageX -= rmWidth + 10;
}
if (pageY + rmHeight > window.innerHeight) {
pageY -= pageY + rmHeight - window.innerHeight;
}
rm.showRightMenu(true, pageY, pageX);
document.getElementById("rightmenu-mask").style.display = "flex";
return false;
}
};
// 监听右键初始化
window.oncontextmenu = oncontextmenuFunction
// 下载图片状态
rm.downloadimging = false;
// 复制图片到剪贴板
rm.writeClipImg = function (imgsrc) {
console.log("按下复制");
rm.hideRightMenu();
anzhiyu.snackbarShow("正在下载中,请稍后", false, 10000);
if (rm.downloadimging == false) {
rm.downloadimging = true;
setTimeout(function () {
copyImage(imgsrc);
anzhiyu.snackbarShow("复制成功!图片已添加盲水印,请遵守版权协议");
rm.downloadimging = false;
}, "10000");
}
};
function imageToBlob(imageURL) {
const img = new Image();
const c = document.createElement("canvas");
const ctx = c.getContext("2d");
img.crossOrigin = "";
img.src = imageURL;
return new Promise(resolve => {
img.onload = function () {
c.width = this.naturalWidth;
c.height = this.naturalHeight;
ctx.drawImage(this, 0, 0);
c.toBlob(
blob => {
// here the image is a blob
resolve(blob);
},
"image/png",
0.75
);
};
});
}
async function copyImage(imageURL) {
const blob = await imageToBlob(imageURL);
const item = new ClipboardItem({ "image/png": blob });
navigator.clipboard.write([item]);
}
rm.switchDarkMode = function () {
// Switch Between Light And Dark Mode
const nowMode = document.documentElement.getAttribute("data-theme") === "dark" ? "dark" : "light";
if (nowMode === "light") {
activateDarkMode();
saveToLocal.set("theme", "dark", 2);
GLOBAL_CONFIG.Snackbar !== undefined && anzhiyu.snackbarShow(GLOBAL_CONFIG.Snackbar.day_to_night);
document.querySelector(".menu-darkmode-text").textContent = "浅色模式";
} else {
activateLightMode();
saveToLocal.set("theme", "light", 2);
GLOBAL_CONFIG.Snackbar !== undefined && anzhiyu.snackbarShow(GLOBAL_CONFIG.Snackbar.night_to_day);
document.querySelector(".menu-darkmode-text").textContent = "深色模式";
}
// handle some cases
typeof runMermaid === "function" && window.runMermaid();
rm.hideRightMenu();
anzhiyu.darkModeStatus();
};
rm.copyUrl = function (id) {
const input = document.createElement("input"); // Create a new <input> element
input.id = "copyVal"; // Set the id of the new element to "copyVal"
document.body.appendChild(input); // Append the new element to the end of the <body> element
const text = id;
input.value = text;
input.select();
input.setSelectionRange(0, input.value.length);
document.execCommand("copy");
input.remove(); // Remove the <input> element from the DOM