2023年12月8日发(作者:)
c语言try语句
try语句是C语言中的一个关键字,它用于异常处理。在try语句中,我们可以放置一段代码,如果这段代码出现了异常,可以通过catch语句来捕获并处理异常。下面是一些关于C语言try语句的例子:
1. 简单的try-catch语句
一个简单的try-catch语句可以用来捕获一些异常,比如除数为0的情况。以下是一个示例程序:
```
#include
int main() {
int a = 10, b = 0, c;
try {
c = a / b;
} catch {
printf("Exception caught: Division by zero.n");
}
return 0;
}
``` 在这个程序中,我们将变量a除以变量b,由于b的值为0,所以会抛出一个异常。在try语句块中,我们尝试执行除法操作,并在catch语句块中捕获异常。在这个示例程序中,输出的结果为“Exception caught: Division by zero.”。
2. 带有多个catch块的try语句
在实际开发中,一个try语句可能会抛出多种不同的异常,我们可以使用多个catch块来捕获这些异常。以下是一个示例程序:
```
#include
int main() {
int a = 10, b = 0, c;
try {
c = a / b;
} catch (DivideByZeroException) {
printf("Exception caught: Division by zero.n");
} catch (NegativeNumberException) {
printf("Exception caught: Negative number.n");
}
return 0; }
```
在这个程序中,我们定义了两种异常类型:DivideByZeroException和NegativeNumberException。在try语句块中,我们尝试执行除法操作,并在catch块中捕获这些异常。如果异常是DivideByZeroException,我们会输出“Exception
caught: Division by zero.”;如果异常是NegativeNumberException,我们会输出“Exception caught:
Negative number.”。
3. 带有finally块的try语句
try语句还可以带有一个finally块,不管try语句是否抛出异常,finally块中的代码都会被执行。以下是一个示例程序:
```
#include
int main() {
int a = 10, b = 0, c;
try {
c = a / b;
} catch (DivideByZeroException) { printf("Exception caught: Division by zero.n");
} catch (NegativeNumberException) {
printf("Exception caught: Negative number.n");
} finally {
printf("Finally block executed.n");
}
return 0;
}
```
在这个程序中,无论try语句是否抛出异常,finally块中的代码都会被执行。在这个示例程序中,输出的结果为“Exception caught:
Division by zero.”和“Finally block executed.”。
4. 带有throw语句的try语句
在C语言中,我们可以使用throw语句来抛出一个异常。以下是一个示例程序:
```
#include
int main() {
int a = 10, b = 0, c; try {
if (b == 0) {
throw DivideByZeroException;
}
c = a / b;
} catch (DivideByZeroException) {
printf("Exception caught: Division by zero.n");
} finally {
printf("Finally block executed.n");
}
return 0;
}
```
在这个程序中,我们在try语句块中加入了一个if语句,用来判断变量b是否为0。如果b为0,我们就使用throw语句抛出一个DivideByZeroException异常。在catch块中,我们捕获这个异常并输出一条消息。在finally块中,我们输出“Finally block
executed.”。
5. 带有自定义异常类的try语句
在实际开发中,我们可能需要自定义一些异常类来处理特定的异常。以下是一个示例程序:
```
#include
class MyException {
public:
const char* message;
MyException(const char* msg) : message(msg) {}
};
int main() {
int a = 10, b = 0, c;
try {
if (b == 0) {
throw MyException("Division by zero.");
}
c = a / b;
} catch (MyException e) {
printf("Exception caught: %sn", e);
} finally {
printf("Finally block executed.n");
} return 0;
}
```
在这个程序中,我们定义了一个自定义异常类MyException,它包含了一个message属性,用来存储异常信息。在try语句块中,我们使用throw语句抛出一个MyException异常。在catch块中,我们捕获这个异常并输出异常信息。在finally块中,我们输出“Finally block executed.”。
6. 带有嵌套try-catch块的try语句
在实际开发中,我们可能需要在一个try语句块中嵌套另外一个try-catch块来处理更加复杂的异常情况。以下是一个示例程序:
```
#include
int main() {
int a = 10, b = 0, c;
try {
try {
if (b == 0) {
throw DivideByZeroException; }
c = a / b;
} catch (DivideByZeroException) {
printf("Inner catch: Division by zero.n");
}
} catch (DivideByZeroException) {
printf("Outer catch: Division by zero.n");
} finally {
printf("Finally block executed.n");
}
return 0;
}
```
在这个程序中,我们在外层try语句块中捕获DivideByZeroException异常,在内层try语句块中也捕获DivideByZeroException异常。在内层catch块中,我们输出“Inner catch: Division by zero.”;在外层catch块中,我们输出“Outer catch: Division by zero.”。在finally块中,我们输出“Finally block executed.”。
7. 带有多个嵌套try-catch块的try语句
在实际开发中,我们可能需要在一个try语句块中嵌套多个try-catch块来处理更加复杂的异常情况。以下是一个示例程序:
```
#include
int main() {
int a = 10, b = 0, c;
try {
try {
try {
if (b == 0) {
throw DivideByZeroException;
}
c = a / b;
} catch (DivideByZeroException) {
printf("Innermost catch: Division by zero.n");
}
} catch (DivideByZeroException) {
printf("Inner catch: Division by zero.n");
}
} catch (DivideByZeroException) {
printf("Outer catch: Division by zero.n"); } finally {
printf("Finally block executed.n");
}
return 0;
}
```
在这个程序中,我们在外层try语句块中捕获DivideByZeroException异常,在内层try语句块中也捕获DivideByZeroException异常,在最内层try语句块中捕获DivideByZeroException异常。在最内层catch块中,我们输出“Innermost catch: Division by zero.”;在内层catch块中,我们输出“Inner catch: Division by zero.”;在外层catch块中,我们输出“Outer catch: Division by zero.”。在finally块中,我们输出“Finally block executed.”。
8. 处理其他类型的异常
除了可以处理自定义的异常类型外,我们还可以处理其他类型的异常。以下是一个示例程序:
```
#include
#include
int a = 10, b = 0, c;
try {
c = a / b;
} catch (int e) {
printf("Exception caught: %dn", e);
} catch (...) {
printf("Unknown exception caught.n");
}
return 0;
}
```
在这个程序中,我们使用了两个catch块来处理异常。第一个catch块用来处理int类型的异常,第二个catch块用来处理其他类型的异常。在try语句块中,我们尝试执行除法操作,由于b的值为0,所以会抛出一个异常。在第一个catch块中,我们将异常的值输出;在第二个catch块中,我们输出“Unknown exception
caught.”。
9. 处理多个异常
在实际开发中,我们可能需要处理多个异常,以下是一个示例程序: ```
#include
class DivideByZeroException {};
class NegativeNumberException {};
int main() {
int a = -10, b = 0, c;
try {
if (b == 0) {
throw DivideByZeroException();
}
if (a < 0) {
throw NegativeNumberException();
}
c = a / b;
} catch (DivideByZeroException e) {
printf("Exception caught: Division by zero.n");
} catch (NegativeNumberException e) {
printf("Exception caught: Negative number.n");
} catch (...) {
printf("Unknown exception caught.n"); }
return 0;
}
```
在这个程序中,我们定义了两个异常类型:DivideByZeroException和NegativeNumberException。在try语句块中,我们使用throw语句抛出这两种异常。在catch块中,我们分别捕获这两种异常并输出异常信息。在最后一个catch块中,我们输出“Unknown exception caught.”。
10. 处理嵌套异常
在实际开发中,我们可能需要处理嵌套的异常情况,以下是一个示例程序:
```
#include
class DivideByZeroException {};
class NegativeNumberException {};
int main() {
int a = -10, b = 0, c;
try { try {
if (b == 0) {
throw DivideByZeroException();
}
if (a < 0) {
throw NegativeNumberException();
}
c = a / b;
} catch (DivideByZeroException e) {
printf("Inner catch: Division by zero.n");
throw e;
}
} catch (DivideByZeroException e) {
printf("Outer catch: Division by zero.n");
} catch (NegativeNumberException e) {
printf("Exception caught: Negative number.n");
} catch (...) {
printf("Unknown exception caught.n");
}
return 0;
}
``` 在这个程序中,我们定义了两个异常类型:DivideByZeroException和NegativeNumberException。在try语句块中,我们使用throw语句抛出这两种异常。在内层try语句块中,我们捕获DivideByZeroException异常,并使用throw语句重新抛出这个异常。在外层try语句块中,我们捕获DivideByZeroException异常,并输出“Outer catch: Division by
zero.”。在其他catch块中,我们分别捕获NegativeNumberException异常和其他类型的异常,然后输出相应的信息。


发布评论