1280 views|0 replies

1662

Posts

0

Resources
The OP
 

C language capture error mechanism [Copy link]

There are generally several ways to handle exceptions in C language: 1. Use the standard C library to provide two functions, abort() and exit(), which can forcibly terminate the program.2. Use the assert macro call, located in the header fileIn the program, when an error occurs, an abort() is triggered. 3. Use the errno global variable, which is provided by the C runtime library function and is located in the header file4. Use goto statement to jump when an error occurs. 5. Use setjmp and longjmp for exception handling. Here is an example of exit method. 1 #include2 #include 3 double diva(double num1,double num2) //Two number division function 4 { 5 double re; 6 re=num1/num2; 7 return re; 8 } 9 int main() 10 { 11 double a,b,result; 12 printf("Please enter the first number: "); 13 scanf("%lf",&a); 14 printf("Please enter the second number: "); 15 scanf("%lf",&b); 16 if(0==b) //If the divisor is 0, terminate the program 17 exit(EXIT_FAILURE); 18 result=diva(a,b); 19 printf("The result of the division is: %.2lf\n",result); 20 return 0; 21 }

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list