-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_errors_data.json
More file actions
1304 lines (1304 loc) · 41.9 KB
/
python_errors_data.json
File metadata and controls
1304 lines (1304 loc) · 41.9 KB
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
{
"version": "1.0.0",
"last_updated": "2025-01-18",
"categories": [
{
"id": "base",
"name_en": "Base Exceptions",
"name_zh": "基础异常",
"description_zh": "所有异常的基类和系统级异常"
},
{
"id": "arithmetic",
"name_en": "Arithmetic Errors",
"name_zh": "算术错误",
"description_zh": "数值计算相关的错误"
},
{
"id": "lookup",
"name_en": "Lookup Errors",
"name_zh": "查找错误",
"description_zh": "数据查询和索引相关的错误"
},
{
"id": "syntax",
"name_en": "Syntax Errors",
"name_zh": "语法错误",
"description_zh": "代码语法相关的错误"
},
{
"id": "type_value",
"name_en": "Type and Value Errors",
"name_zh": "类型和值错误",
"description_zh": "类型不匹配和值无效的错误"
},
{
"id": "system",
"name_en": "System Errors",
"name_zh": "系统错误",
"description_zh": "操作系统和环境相关的错误"
},
{
"id": "import",
"name_en": "Import Errors",
"name_zh": "导入错误",
"description_zh": "模块导入相关的错误"
},
{
"id": "unicode",
"name_en": "Unicode Errors",
"name_zh": "Unicode错误",
"description_zh": "字符编码相关的错误"
},
{
"id": "warnings",
"name_en": "Warnings",
"name_zh": "警告",
"description_zh": "各种警告信息"
}
],
"exceptions": [
{
"id": "base_exception",
"exception_name": "BaseException",
"category_id": "base",
"description_en": "The base class for all built-in exceptions",
"description_zh": "所有内置异常的基类",
"inheritance": [],
"common_messages": [
{
"message_en": "BaseException",
"message_zh": "基础异常",
"scenario": "所有异常的根类,通常不直接使用",
"example_code": "raise BaseException('Something went wrong')",
"solution": "使用更具体的异常类型,如Exception或其子类"
}
],
"related_exceptions": [
"Exception"
],
"tags": [
"base",
"root"
]
},
{
"id": "system_exit",
"exception_name": "SystemExit",
"category_id": "base",
"description_en": "Raised by the sys.exit() function",
"description_zh": "由sys.exit()函数引发,Python解释器请求退出",
"inheritance": [
"BaseException"
],
"common_messages": [
{
"message_en": "SystemExit: 0",
"message_zh": "系统退出:0",
"scenario": "程序正常退出",
"example_code": "import sys\nsys.exit(0)",
"solution": "这是正常的程序退出,通常不需要处理"
}
],
"related_exceptions": [
"KeyboardInterrupt"
],
"tags": [
"system",
"exit"
]
},
{
"id": "keyboard_interrupt",
"exception_name": "KeyboardInterrupt",
"category_id": "base",
"description_en": "Raised when the user hits the interrupt key (normally Control-C)",
"description_zh": "用户中断执行时引发(通常是按Ctrl+C)",
"inheritance": [
"BaseException"
],
"common_messages": [
{
"message_en": "KeyboardInterrupt",
"message_zh": "键盘中断",
"scenario": "用户按Ctrl+C中断程序执行",
"example_code": "# 长时间运行的循环\nwhile True:\n pass # 按Ctrl+C会触发",
"solution": "可以用try-except捕获来优雅地处理程序中断"
}
],
"related_exceptions": [
"SystemExit"
],
"tags": [
"interrupt",
"user"
]
},
{
"id": "zero_division_error",
"exception_name": "ZeroDivisionError",
"category_id": "arithmetic",
"description_en": "Raised when the second argument of a division or modulo operation is zero",
"description_zh": "除法或取模运算的第二个参数为零时引发",
"inheritance": [
"ArithmeticError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "division by zero",
"message_zh": "除零错误",
"scenario": "进行除法运算时分母为0",
"example_code": "result = 10 / 0",
"solution": "在除法前检查分母是否为0,或使用try-except处理"
},
{
"message_en": "integer division or modulo by zero",
"message_zh": "整数除法或取模运算除零",
"scenario": "整数除法或取模运算时除数为0",
"example_code": "result = 10 % 0",
"solution": "确保除数不为0,添加条件判断"
}
],
"related_exceptions": [
"ArithmeticError",
"OverflowError"
],
"tags": [
"arithmetic",
"division",
"common"
]
},
{
"id": "type_error",
"exception_name": "TypeError",
"category_id": "type_value",
"description_en": "Raised when an operation or function is applied to an object of inappropriate type",
"description_zh": "对不适当类型的对象执行操作或函数时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "unsupported operand type(s) for +: 'int' and 'str'",
"message_zh": "不支持的操作数类型:'int' 和 'str' 不能进行 + 运算",
"scenario": "尝试对不兼容的类型进行运算",
"example_code": "result = 5 + 'hello'",
"solution": "确保操作数类型兼容,或进行类型转换"
},
{
"message_en": "'str' object is not callable",
"message_zh": "'str' 对象不可调用",
"scenario": "尝试调用非函数对象",
"example_code": "name = 'John'\nresult = name()",
"solution": "检查对象是否为函数,或修正调用语法"
}
],
"related_exceptions": [
"ValueError",
"AttributeError"
],
"tags": [
"type",
"common",
"operation"
]
},
{
"id": "value_error",
"exception_name": "ValueError",
"category_id": "type_value",
"description_en": "Raised when an operation or function receives an argument with the right type but an inappropriate value",
"description_zh": "当操作或函数接收到类型正确但值不合适的参数时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "invalid literal for int() with base 10: 'abc'",
"message_zh": "int()函数在基数10下的无效字面量:'abc'",
"scenario": "尝试将非数字字符串转换为整数",
"example_code": "number = int('abc')",
"solution": "确保字符串包含有效的数字,或使用try-except处理转换错误"
},
{
"message_en": "math domain error",
"message_zh": "数学域错误",
"scenario": "数学函数接收到域外的值",
"example_code": "import math\nresult = math.sqrt(-1)",
"solution": "检查输入值是否在函数的有效域内"
}
],
"related_exceptions": [
"TypeError",
"OverflowError"
],
"tags": [
"value",
"conversion",
"common"
]
},
{
"id": "index_error",
"exception_name": "IndexError",
"category_id": "lookup",
"description_en": "Raised when a sequence subscript is out of range",
"description_zh": "当序列下标超出范围时引发",
"inheritance": [
"LookupError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "list index out of range",
"message_zh": "列表索引超出范围",
"scenario": "访问列表中不存在的索引",
"example_code": "my_list = [1, 2, 3]\nvalue = my_list[5]",
"solution": "检查索引是否在有效范围内,或使用try-except处理"
},
{
"message_en": "string index out of range",
"message_zh": "字符串索引超出范围",
"scenario": "访问字符串中不存在的索引",
"example_code": "text = 'hello'\nchar = text[10]",
"solution": "确保索引小于字符串长度"
}
],
"related_exceptions": [
"KeyError",
"LookupError"
],
"tags": [
"index",
"sequence",
"common"
]
},
{
"id": "key_error",
"exception_name": "KeyError",
"category_id": "lookup",
"description_en": "Raised when a mapping (dictionary) key is not found",
"description_zh": "当映射(字典)中找不到键时引发",
"inheritance": [
"LookupError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "'key_name'",
"message_zh": "'键名'",
"scenario": "访问字典中不存在的键",
"example_code": "my_dict = {'a': 1, 'b': 2}\nvalue = my_dict['c']",
"solution": "使用dict.get()方法或先检查键是否存在"
}
],
"related_exceptions": [
"IndexError",
"AttributeError"
],
"tags": [
"key",
"dictionary",
"common"
]
},
{
"id": "attribute_error",
"exception_name": "AttributeError",
"category_id": "type_value",
"description_en": "Raised when an attribute reference or assignment fails",
"description_zh": "当属性引用或赋值失败时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "'str' object has no attribute 'append'",
"message_zh": "'str' 对象没有 'append' 属性",
"scenario": "尝试访问对象不存在的属性或方法",
"example_code": "text = 'hello'\ntext.append('world')",
"solution": "检查对象类型和可用的属性/方法"
},
{
"message_en": "module 'math' has no attribute 'pi2'",
"message_zh": "模块 'math' 没有 'pi2' 属性",
"scenario": "访问模块中不存在的属性",
"example_code": "import math\nvalue = math.pi2",
"solution": "检查模块文档确认正确的属性名"
}
],
"related_exceptions": [
"TypeError",
"NameError"
],
"tags": [
"attribute",
"object",
"common"
]
},
{
"id": "name_error",
"exception_name": "NameError",
"category_id": "type_value",
"description_en": "Raised when a local or global name is not found",
"description_zh": "当找不到本地或全局名称时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "name 'variable_name' is not defined",
"message_zh": "名称 'variable_name' 未定义",
"scenario": "使用未定义的变量",
"example_code": "print(undefined_variable)",
"solution": "确保变量在使用前已定义,或检查变量名拼写"
}
],
"related_exceptions": [
"UnboundLocalError",
"AttributeError"
],
"tags": [
"name",
"variable",
"common"
]
},
{
"id": "import_error",
"exception_name": "ImportError",
"category_id": "import",
"description_en": "Raised when an import statement fails to find the module definition",
"description_zh": "当import语句找不到模块定义时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "No module named 'module_name'",
"message_zh": "没有名为 'module_name' 的模块",
"scenario": "导入不存在的模块",
"example_code": "import non_existent_module",
"solution": "检查模块名拼写,确保模块已安装或在Python路径中"
},
{
"message_en": "cannot import name 'function_name' from 'module_name'",
"message_zh": "无法从 'module_name' 导入名称 'function_name'",
"scenario": "从模块导入不存在的函数或类",
"example_code": "from math import non_existent_function",
"solution": "检查要导入的名称是否存在于目标模块中"
}
],
"related_exceptions": [
"ModuleNotFoundError"
],
"tags": [
"import",
"module",
"common"
]
},
{
"id": "syntax_error",
"exception_name": "SyntaxError",
"category_id": "syntax",
"description_en": "Raised when the parser encounters a syntax error",
"description_zh": "当解析器遇到语法错误时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "invalid syntax",
"message_zh": "无效语法",
"scenario": "代码语法不正确",
"example_code": "if True\n print('missing colon')",
"solution": "检查代码语法,确保符合Python语法规则"
},
{
"message_en": "unexpected EOF while parsing",
"message_zh": "解析时遇到意外的文件结束",
"scenario": "代码不完整,缺少结束符号",
"example_code": "def function():\n print('missing closing",
"solution": "检查是否有未闭合的括号、引号或代码块"
}
],
"related_exceptions": [
"IndentationError",
"TabError"
],
"tags": [
"syntax",
"parsing",
"common"
]
},
{
"id": "indentation_error",
"exception_name": "IndentationError",
"category_id": "syntax",
"description_en": "Raised when there is incorrect indentation",
"description_zh": "当缩进不正确时引发",
"inheritance": [
"SyntaxError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "expected an indented block",
"message_zh": "期望一个缩进块",
"scenario": "在需要缩进的地方没有缩进",
"example_code": "if True:\nprint('no indentation')",
"solution": "在冒号后的代码块前添加正确的缩进"
},
{
"message_en": "unindent does not match any outer indentation level",
"message_zh": "取消缩进与任何外层缩进级别都不匹配",
"scenario": "缩进级别不一致",
"example_code": "if True:\n print('4 spaces')\n print('2 spaces')",
"solution": "保持一致的缩进级别,建议使用4个空格"
}
],
"related_exceptions": [
"SyntaxError",
"TabError"
],
"tags": [
"indentation",
"syntax",
"common"
]
},
{
"id": "file_not_found_error",
"exception_name": "FileNotFoundError",
"category_id": "system",
"description_en": "Raised when a file or directory is requested but doesn't exist",
"description_zh": "当请求的文件或目录不存在时引发",
"inheritance": [
"OSError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "[Errno 2] No such file or directory: 'filename.txt'",
"message_zh": "[错误号 2] 没有这样的文件或目录: 'filename.txt'",
"scenario": "尝试打开不存在的文件",
"example_code": "with open('nonexistent.txt', 'r') as f:\n content = f.read()",
"solution": "检查文件路径是否正确,或先检查文件是否存在"
}
],
"related_exceptions": [
"PermissionError",
"IsADirectoryError"
],
"tags": [
"file",
"system",
"common"
]
},
{
"id": "unicode_decode_error",
"exception_name": "UnicodeDecodeError",
"category_id": "unicode",
"description_en": "Raised when a Unicode-related error occurs during decoding",
"description_zh": "在Unicode解码过程中发生错误时引发",
"inheritance": [
"UnicodeError",
"ValueError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "'utf-8' codec can't decode byte 0xff in position 0: invalid start byte",
"message_zh": "'utf-8' 编解码器无法解码位置0的字节0xff:无效的起始字节",
"scenario": "尝试用错误的编码解码字节",
"example_code": "data = b'\\xff\\xfe'\ntext = data.decode('utf-8')",
"solution": "使用正确的编码格式,或指定错误处理方式"
}
],
"related_exceptions": [
"UnicodeEncodeError",
"UnicodeError"
],
"tags": [
"unicode",
"encoding",
"decode"
]
},
{
"id": "exception",
"exception_name": "Exception",
"category_id": "base",
"description_en": "Common base class for all non-exit exceptions",
"description_zh": "所有非退出异常的通用基类",
"inheritance": [
"BaseException"
],
"common_messages": [
{
"message_en": "Exception",
"message_zh": "异常",
"scenario": "用户自定义异常的基类",
"example_code": "raise Exception('Something went wrong')",
"solution": "使用更具体的异常类型,或创建自定义异常类"
}
],
"related_exceptions": [
"BaseException"
],
"tags": [
"base",
"common"
]
},
{
"id": "runtime_error",
"exception_name": "RuntimeError",
"category_id": "base",
"description_en": "Raised when an error is detected that doesn't fall in any of the other categories",
"description_zh": "当检测到不属于任何其他类别的错误时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "maximum recursion depth exceeded",
"message_zh": "超过最大递归深度",
"scenario": "函数递归调用过深",
"example_code": "def recursive():\n return recursive()\nrecursive()",
"solution": "检查递归终止条件,或使用迭代方式替代递归"
}
],
"related_exceptions": [
"RecursionError"
],
"tags": [
"runtime",
"general"
]
},
{
"id": "stop_iteration",
"exception_name": "StopIteration",
"category_id": "base",
"description_en": "Raised by built-in function next() and an iterator's __next__() method to signal that there are no further items",
"description_zh": "由内置函数next()和迭代器的__next__()方法引发,表示没有更多项目",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "StopIteration",
"message_zh": "停止迭代",
"scenario": "迭代器耗尽时调用next()",
"example_code": "it = iter([1, 2])\nnext(it)\nnext(it)\nnext(it) # 第三次调用",
"solution": "使用for循环或try-except处理StopIteration"
}
],
"related_exceptions": [
"GeneratorExit"
],
"tags": [
"iteration",
"generator"
]
},
{
"id": "assertion_error",
"exception_name": "AssertionError",
"category_id": "base",
"description_en": "Raised when an assert statement fails",
"description_zh": "当assert语句失败时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "AssertionError",
"message_zh": "断言错误",
"scenario": "assert语句的条件为False",
"example_code": "assert 1 == 2, 'Numbers are not equal'",
"solution": "检查断言条件是否正确,或修复导致断言失败的逻辑"
}
],
"related_exceptions": [
"ValueError"
],
"tags": [
"assertion",
"testing",
"debug"
]
},
{
"id": "overflow_error",
"exception_name": "OverflowError",
"category_id": "arithmetic",
"description_en": "Raised when the result of an arithmetic operation is too large to be represented",
"description_zh": "当算术运算的结果太大无法表示时引发",
"inheritance": [
"ArithmeticError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "math range error",
"message_zh": "数学范围错误",
"scenario": "数学运算结果超出表示范围",
"example_code": "import math\nmath.exp(1000)",
"solution": "检查输入值范围,使用适当的数据类型或算法"
}
],
"related_exceptions": [
"ZeroDivisionError",
"ArithmeticError"
],
"tags": [
"arithmetic",
"overflow",
"math"
]
},
{
"id": "memory_error",
"exception_name": "MemoryError",
"category_id": "system",
"description_en": "Raised when an operation runs out of memory",
"description_zh": "当操作耗尽内存时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "MemoryError",
"message_zh": "内存错误",
"scenario": "程序使用的内存超过系统限制",
"example_code": "big_list = [0] * (10**10) # 尝试创建巨大列表",
"solution": "优化内存使用,分批处理数据,或增加系统内存"
}
],
"related_exceptions": [
"OverflowError"
],
"tags": [
"memory",
"system",
"resource"
]
},
{
"id": "recursion_error",
"exception_name": "RecursionError",
"category_id": "system",
"description_en": "Raised when the maximum recursion depth is exceeded",
"description_zh": "当超过最大递归深度时引发",
"inheritance": [
"RuntimeError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "maximum recursion depth exceeded",
"message_zh": "超过最大递归深度",
"scenario": "函数无限递归调用",
"example_code": "def factorial(n):\n return n * factorial(n-1) # 缺少终止条件\nfactorial(5)",
"solution": "添加递归终止条件,或使用迭代方式替代递归"
}
],
"related_exceptions": [
"RuntimeError"
],
"tags": [
"recursion",
"stack",
"common"
]
},
{
"id": "os_error",
"exception_name": "OSError",
"category_id": "system",
"description_en": "Raised when a system function returns a system-related error",
"description_zh": "当系统函数返回系统相关错误时引发",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "[Errno 13] Permission denied",
"message_zh": "[错误号 13] 权限被拒绝",
"scenario": "没有权限访问文件或目录",
"example_code": "with open('/root/secret.txt', 'r') as f:\n content = f.read()",
"solution": "检查文件权限,使用sudo或更改文件权限"
}
],
"related_exceptions": [
"FileNotFoundError",
"PermissionError"
],
"tags": [
"system",
"os",
"file"
]
},
{
"id": "permission_error",
"exception_name": "PermissionError",
"category_id": "system",
"description_en": "Raised when trying to run an operation without the adequate access rights",
"description_zh": "当尝试在没有足够访问权限的情况下运行操作时引发",
"inheritance": [
"OSError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "[Errno 13] Permission denied: 'filename'",
"message_zh": "[错误号 13] 权限被拒绝: 'filename'",
"scenario": "尝试访问没有权限的文件",
"example_code": "with open('/etc/passwd', 'w') as f:\n f.write('test')",
"solution": "检查文件权限,使用适当的用户权限或更改文件权限"
}
],
"related_exceptions": [
"OSError",
"FileNotFoundError"
],
"tags": [
"permission",
"file",
"system"
]
},
{
"id": "module_not_found_error",
"exception_name": "ModuleNotFoundError",
"category_id": "import",
"description_en": "Raised when a module could not be located",
"description_zh": "当无法定位模块时引发",
"inheritance": [
"ImportError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "No module named 'requests'",
"message_zh": "没有名为 'requests' 的模块",
"scenario": "导入未安装的第三方模块",
"example_code": "import requests",
"solution": "使用pip安装模块:pip install requests"
},
{
"message_en": "No module named 'mymodule'",
"message_zh": "没有名为 'mymodule' 的模块",
"scenario": "导入不存在的自定义模块",
"example_code": "import mymodule",
"solution": "检查模块名拼写,确保模块文件在Python路径中"
}
],
"related_exceptions": [
"ImportError"
],
"tags": [
"import",
"module",
"common"
]
},
{
"id": "unbound_local_error",
"exception_name": "UnboundLocalError",
"category_id": "type_value",
"description_en": "Raised when a reference is made to a local variable in a function or method, but no value has been bound to that variable",
"description_zh": "当在函数或方法中引用局部变量,但该变量尚未绑定值时引发",
"inheritance": [
"NameError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "local variable 'x' referenced before assignment",
"message_zh": "局部变量 'x' 在赋值前被引用",
"scenario": "在函数中使用变量前未初始化",
"example_code": "def func():\n print(x)\n x = 1\nfunc()",
"solution": "在使用变量前先赋值,或使用global/nonlocal关键字"
}
],
"related_exceptions": [
"NameError"
],
"tags": [
"local",
"variable",
"scope"
]
},
{
"id": "lookup_error",
"exception_name": "LookupError",
"category_id": "lookup",
"description_en": "Base class for exceptions raised when a key or index used on a mapping or sequence is invalid",
"description_zh": "当映射或序列上使用的键或索引无效时引发的异常的基类",
"inheritance": [
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "LookupError",
"message_zh": "查找错误",
"scenario": "查找操作失败的基类",
"example_code": "# 通常不直接使用,而是使用子类如KeyError, IndexError",
"solution": "使用更具体的异常类型如KeyError或IndexError"
}
],
"related_exceptions": [
"KeyError",
"IndexError"
],
"tags": [
"lookup",
"base"
]
},
{
"id": "tab_error",
"exception_name": "TabError",
"category_id": "syntax",
"description_en": "Raised when indentation contains an inconsistent use of tabs and spaces",
"description_zh": "当缩进包含制表符和空格的不一致使用时引发",
"inheritance": [
"IndentationError",
"SyntaxError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "inconsistent use of tabs and spaces in indentation",
"message_zh": "缩进中制表符和空格使用不一致",
"scenario": "混合使用Tab和空格进行缩进",
"example_code": "if True:\n print('spaces') # 4个空格\n\tprint('tab') # 1个制表符",
"solution": "统一使用空格或制表符,推荐使用4个空格"
}
],
"related_exceptions": [
"IndentationError",
"SyntaxError"
],
"tags": [
"indentation",
"tab",
"space"
]
},
{
"id": "unicode_error",
"exception_name": "UnicodeError",
"category_id": "unicode",
"description_en": "Base class for Unicode-related errors",
"description_zh": "Unicode相关错误的基类",
"inheritance": [
"ValueError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "UnicodeError",
"message_zh": "Unicode错误",
"scenario": "Unicode编码/解码错误的基类",
"example_code": "# 通常不直接使用,而是使用子类",
"solution": "使用更具体的Unicode异常类型"
}
],
"related_exceptions": [
"UnicodeDecodeError",
"UnicodeEncodeError"
],
"tags": [
"unicode",
"base"
]
},
{
"id": "unicode_encode_error",
"exception_name": "UnicodeEncodeError",
"category_id": "unicode",
"description_en": "Raised when a Unicode-related error occurs during encoding",
"description_zh": "在Unicode编码过程中发生错误时引发",
"inheritance": [
"UnicodeError",
"ValueError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "'ascii' codec can't encode character '中' in position 0: ordinal not in range(128)",
"message_zh": "'ascii' 编解码器无法编码位置0的字符'中':序数不在范围(128)内",
"scenario": "尝试用ASCII编码中文字符",
"example_code": "text = '中文'\nencoded = text.encode('ascii')",
"solution": "使用支持中文的编码如UTF-8:text.encode('utf-8')"
}
],
"related_exceptions": [
"UnicodeDecodeError",
"UnicodeError"
],
"tags": [
"unicode",
"encoding",
"encode"
]
},
{
"id": "is_a_directory_error",
"exception_name": "IsADirectoryError",
"category_id": "system",
"description_en": "Raised when a file operation is requested on a directory",
"description_zh": "当对目录请求文件操作时引发",
"inheritance": [
"OSError",
"Exception",
"BaseException"
],
"common_messages": [
{
"message_en": "[Errno 21] Is a directory: 'dirname'",
"message_zh": "[错误号 21] 是一个目录: 'dirname'",
"scenario": "尝试以文件方式打开目录",