异常的统一处理

我们在编写程序的时候往往都会抛出不可预期的异常,如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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值