异常的统一处理

来源:互联网 发布:ubuntu 编译php 编辑:程序博客网 时间:2024/05/01 01:42

我们在编写程序的时候往往都会抛出不可预期的异常,如c中常见的内存不足异常,java中的类型转换异常。因此,我们处理异常往往会有以下的代码形式:

java语言:

  1. try {
  2. URL url = new URL("cym.so");
  3. } catch (MalformedURLException e) {
  4. // TODO Auto-generated catch block
  5. e.printStackTrace();
  6. }

### c语言:

  1. if( p == NULL)
  2. {
  3. printf( "ERR: The pointer is NULL\n" );
  4. }

用陈浩老师的话讲,上面的属于学生时代的编程,很不利于维护和管理。

对于出错信息和提示信息,应该统一处理。而不是写成“硬编码”。

我总结了java和c两种语言的异常处理的通用方法,发现在项目中运用起来作用还是不小的。


JAVA:

1.建立一个继承Exception的公用的异常类,声明一些常用的异常。

  1. public class CustomException extends Exception{
  2. /** NO_ERROR 8*/
  3. public static final int NO_ERROR =0;
  4. /** UNKNOW_ERROR */
  5. public static final int UNKNOWN_ERROR = 1;
  6. /** IO_ERROR */
  7. public static final int NET_IO_ERROR = 2;
  8. /** INVALID_LITERAL_LENGTH */
  9. public static final int INVALID_LITERAL_LENGTH = 3;
  10. /** SERVER_CLOSE_CONNECTION */
  11. public static final int SERVER_CLOSE_CONNECTION = 4;
  12. /** JSON_TRANFORM_FAILED */
  13. public static final int JSON_TRANFORM_FAILED = 5;
  14. public static final int TRANSITION_EXCEPTION = 6;
  15. }

2. 为公用的异常类提供几个常用的构造函数:

  1. /**
  2. *
  3. * @param errorType the type of exception
  4. * @param errorMsg the msg of exception
  5. * @param errorData the extra data of exception
  6. */
  7. public CustomException (int errorType, String errorMsg, Object errorData){
  8. super(errorMsg);
  9. this.errorType = errorType;
  10. this.errorData = errorData;
  11. }
  12. /**
  13. * @param type the type of exception.
  14. */
  15. public CustomException(int type) {
  16. this(type, "", null);
  17. }
  18. /**
  19. * @param type the type of exception.
  20. * @param message the message of exception.
  21. * @param throwable the throwable object.
  22. */
  23. public CustomException(int type, String message, Throwable throwable) {
  24. super(message, throwable);
  25. errorType = type;
  26. errorData = null;
  27. }
  28. /**
  29. * @param message the message of exception.
  30. * @param throwable the throwable object.
  31. */
  32. public CustomException(String message, Throwable throwable) {
  33. this(UNKNOWN_ERROR, message, throwable);
  34. }

3. 有异常发生时的调用方法

  1. try {
  2. URL url = new URL("cym.so");
  3. } catch (MalformedURLException e) {
  4. throw new CustomException(CustomException.TRANSITION_EXCEPTION,"url转换错误",e);
  5. }

4.执行效果

http://cdn.saymagic.cn/140928114156.png


C语言

1.声明出错代码:

  1. # define ERR_NO_ERROR 0 /* NO ERROR */
  2. # define UNKNOWN_ERROR 1 /* UNKNOWN ERROR */
  3. # define MEMEROY_IS_NOT_ENOUGH 2 /* MEMEROY IS NOT ENOUGH */
  4. # define PERMISSION_DENIED 3 /* PERMISSION DENIED */
  5. # define BAD_CONFIGURATION_FILE_FORMAT 4/* BAD CONFIGURATION FILE FORMAT */
  6. # define TIME_OUT 5 /* TIME OUT */

2.声明出错信息

  1. const char* errmsg[] = {
  2. "NO ERROR",
  3. "UNKNOWN ERROR",
  4. "MEMEROY IS NOT ENOUGH",
  5. "PERMISSION DENIED",
  6. "BAD CONFIGURATION FILE FORMAT",
  7. "TIME OUT"
  8. };

3.声明出错代码全局变量

  1. long errorno = 0;

4.打印出错信息函数

  1. void perror(char* info)
  2. {
  3. if( info )
  4. {
  5. printf("%s: %s\n", info, errmsg[errorno]);
  6. return;
  7. }
  8. printf("Error: %s\n", errmsg[errorno]);
  9. }

5.调用方式

  1. char *p = (char *)malloc(sizeof(char *));
  2. if(!p)
  3. {
  4. errorno = 2;
  5. perror("main()");
  6. }
  7. free(p);


原文地址:http://blog.saymagic.cn/2014/09/28/%E5%BC%82%E5%B8%B8%E7%9A%84%E7%BB%9F%E4%B8%80%E5%A4%84%E7%90%86.html

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 新买的绣球蔫了怎么办 绣球花被太阳晒阉了怎么办 羊肉香精放多了怎么办 被飞机防腐剂弄到皮肤怎么办 狗吃了脱氧保鲜剂呕吐怎么办 小孩误吃试纸了保鲜剂怎么办 狗狗把保鲜剂吃了怎么办 小孩吃了防潮珠怎么办 狗吃了防潮剂怎么办 洋桔梗有点烂根怎么办 变色球花枯萎了怎么办 桔梗花叶子蔫了怎么办 洋桔梗头垂下来怎么办 洋桔梗花容易折断怎么办 眼睛被火炮炸伤了怎么办 逆水寒包裹满了怎么办 逆水寒包裹里满了怎么办 grim soul包裹满了怎么办 剑三包裹满了怎么办 电脑开机后都是英文怎么办 欠员工工资仲裁老板不到庭怎么办 乔丹拖鞋鞋底硬怎么办 公牛插座电阻烧了怎么办 公牛led灯太刺眼怎么办 公牛插座usb坏了怎么办 墙壁上开关坏了怎么办 刑事二审判决后不服的怎么办 空调外机空间小怎么办 离婚判决生效书丢了怎么办 法院判决离婚对方上诉怎么办 法院判决离婚上诉中级法院怎么办 刑诉中二审发现一审事实错误怎么办 法院判决过了执行期怎么办 刑事犯罪判决后法院未执行怎么办 法院起诉对方不签字怎么办 法院判离婚对方不要签字怎么办 老人不能到法院签字怎么办 法院判决书被告人没签字怎么办? 法院判决离婚一方不签字怎么办 法院判决离婚对方不签字怎么办 法院已判决被告没有钱怎么办