-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sonder-joker
committed
Apr 24, 2024
1 parent
515440d
commit ea26c58
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
title: "异常与错误处理" | ||
date: 2024-01-27T16:23:20+08:00 | ||
draft: true | ||
--- | ||
|
||
# 为什么需要异常处理 | ||
在编程经验尚浅的时候,我总是不能很好的正常处理程序中的异常和错误。 | ||
先下定义: | ||
异常是什么:当发生的时候,程序已经无法恢复了,一般大家会说分配内存失败这种问题。但还有一些值得考虑的,比如app中的某个页面出现异常 | ||
错误是什么:是正常的应该兜底的出错逻辑,比如网络请求失败,文件读取失败等等。 | ||
|
||
处理程序中的错误不是一件简单的事情。 | ||
|
||
## 从C语言开始 | ||
作为大部分人接触的第一门语言,C语言拥有着最简单的异常处理方式。 | ||
unix style的错误处理方式如下 | ||
```c | ||
int open(const char *pathname, int flags, ... /* mode_t mode */); | ||
|
||
int fd = open("file", O_RDONLY); | ||
if (fd < 0) { | ||
perror("open"); | ||
exit(1); | ||
} | ||
``` | ||
这个错误处理方式有一定的问题 | ||