首页 技术 正文
技术 2022年11月8日
0 收藏 569 点赞 1,253 浏览 2291 个字

目录

1. 引入RuntimeException

public class RuntimeException {    public static void main(String[] args) {
// TODO Auto-generated method stub
String str="123";
int temp=Integer.parseInt(str);
System.out.println(temp*temp);
}
}

产看parseInt方法的源代码如下:


public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}

我们发现这个方法中抛出了NumberFormatException异常,但是在上面的代码中我们没有找到try…catch来处理,这是为什么呢。按照我们异常处理的知识,如果一个方法通过throws抛出了异常,那么可以在抛出异常的方法中不适用try…catch,但是在调用这个方法的地方必须有try…catch来处理。

2. 下面来观察NumberFormatException类的继承关系:

从上图我们可以发现NumberFormatException是RuntimeException的子类,那么这就需要我们清楚Exception和RuntimeException的概念:

  1. Exception:在程序中必须使用try…catch进行处理。
  2. RuntimeException:可以不使用try…catch进行处理,但是如果有异常产生,则异常将由JVM进行处理。

对于RuntimeException的子类最好也使用异常处理机制。虽然RuntimeException的异常可以不使用try…catch进行处理,但是如果一旦发生异常,则肯定会导致程序中断执行,所以,为了保证程序再出错后依然可以执行,在开发代码时最好使用try…catch的异常处理机制进行处理。

3.自定义异常

下面给出一个自定义异常的实例:

class MyException extends Exception{
public MyException(String msg){
super(msg);
}
}public class DefaultException { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
throw new MyException("自定义异常");
}catch(Exception e){
System.err.println(e);// }
}
}

输出结果为:

MyException: 自定义异常

4. 常见的RuntimeException

RuntimeException是开发中最容易遇到的,下面列举一下常见的RuntimeException:

1、NullPointerException:见的最多了,其实很简单,一般都是在null对象上调用方法了。

    String s=null;
boolean eq=s.equals(""); // NullPointerException public int getNumber(String str){
   if(str.equals("A")) return 1;
   else if(str.equals("B")) return 2;
}

这个方法就有可能抛出NullPointerException,我建议你主动抛出异常,因为代码一多,你可能又晕了。

   public int getNumber(String str) {
if(str==null) throw new NullPointerException("参数不能为空");
   if(str.equals("A")) return 1;
   else if(str.equals("B")) return 2;
}

2、NumberFormatException:继承IllegalArgumentException,字符串转换为数字时出现。比如int i= Integer.parseInt("ab3");

3、ArrayIndexOutOfBoundsException:数组越界。比如 int[] a=new int[3]; int b=a[3];

4、StringIndexOutOfBoundsException:字符串越界。比如 String s="hello"; char c=s.chatAt(6);

5、ClassCastException:类型转换错误。比如 Object obj=new Object(); String s=(String)obj;

6、UnsupportedOperationException:该操作不被支持。如果我们希望不支持这个方法,可以抛出这个异常。既然不支持还要这个干吗?有可能子类中不想支持父类中有的方法,可以直接抛出这个异常。

7、ArithmeticException:算术错误,典型的就是0作为除数的时候。

8、IllegalArgumentException:非法参数,在把字符串转换成数字的时候经常出现的一个异常,我们可以在自己的程序中好好利用这个异常。

本文转自:http://blog.csdn.net/mr_pang/article/details/49624425

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291