先说几句题外话,为什么会想到开始写博客呢。主要原因还是受打击了。
上周五去搜狐面试,问了一些基础问题,因为好久都没用用到基础知识,一时回答不出来,面试官跟我说,不管后面学的如何,基础一定要过硬。所以,回来就回顾了一下基础知识,顺便把最近回顾的,做个总结,以方便以后查看。
----------------------------------------------------------------------------------------------------------------------
异常分类:
1.严重异常:Error
2.运行时异常
3.编译时异常
严重异常:Error
一般这样的异常,是用代码不能去处理的,比如内存溢出,我们只能增加内存,但是不能通过代码去增加,所以这样的异常,我们一般不作处理。
运行时异常:RuntimeException
运行时异常,就是在程序执行的过程中遇到问题,jvm自动帮我们去作出处理。出现这样的异常,主要原因是我们书写不规范,或者说是一时大意造成的异常。我大概列举一下我常见到的运行时异常的种类。
1.NullPointException --> 空指针异常
这个异常我相信是见得最多的,但也是最好解决的一个异常。主要意思就是在某个地方,操作某个对象时,发现对象时空的,最常见的是传参问题,当然在后面的Springmvc中也长见到,比如Service层出现 aaaService为空。举个简单例子说明一下这个异常:
<span style="font-size:18px;">public class NullPointExceptionDemo {
public static void main(String[] args) {
Integer result = fun(null);
System.out.println(result);
}
public static Integer fun(Integer a){
Integer result = null;
result = a.intValue();
return result;
}
}</span>
当a为空的时候,调用Integer的某个方法,就会提示空指针异常。
2.ClassCastException --> 类型转换异常
这个异常主要是类型转换出错,当你创建一个Object的集合是,在其中添加了一个String对象,当你需要接受一个Integer类的时候,却传来的是String类,而String类型是不能直接用Integer去接受并强转的。因为集合是Object类型,所以可以往集合中添加任何类型的元素。举个例子:
<span style="font-size:18px;">public class ClassCastExceptionDemo {
public static void main(String[] args) {
List<Object> objList = new ArrayList<Object>();
objList.add("abc");
Integer result = (Integer) objList.get(0);
System.out.println(result);
}
}</span>
3.IndexOutofBoundsException --> 数组越界异常
这个异常很好理解,数组长度是固定的,当长度超出范围后,就会出现这样的异常。
<span style="font-size:18px;">public class IndexOutOfBoundsExceptionDemo {
public static void main(String[] args) {
String[] str = new String[2];
str[0] = "abc";
str[1] = "bcd";
str[2] = "cde";
for (String string : str) {
System.out.println(string);
}
}
}</span>
4.IllegalArgumentException --> 非法参数异常
这个异常在基础部分很少见到,在web开发中经常会出现这样的异常。代码比较多,后期遇到我再补充。
今天学习回顾线程的时候,正好遇到了这个异常,顺便把这儿补充了。
线程有一个优先级的设置,Thread setPriority(int i)。这个优先级,默认情况下大小是5,可以设置的范围,看源码,是1-10,所以,如果在这儿设置一个大于10或者小于1的数值,将会出现这个非法参数异常。
下面是线程setPriority源码:
public final void setPriority(int newPriority) {
ThreadGroup g;
checkAccess();
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
setPriority0(priority = newPriority);
}
}
现在做个小测试:
public class ThreadPriority extends Thread {
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(getName()+"-----"+i);
}
}
public static void main(String[] args) {
ThreadPriority tp1 = new ThreadPriority();
ThreadPriority tp2 = new ThreadPriority();
ThreadPriority tp3 = new ThreadPriority();
tp1.setPriority(100);
tp1.setPriority(1);
tp1.setPriority(2);
tp1.start();
tp2.start();
tp3.start();
}
}
输出结果:
Exception in thread "main" java.lang.IllegalArgumentException
at java.lang.Thread.setPriority(Thread.java:1086)
at thread_1.ThreadPriority.main(ThreadPriority.java:16)
编译时异常:
非运行时异常的异常为编译时。这样的异常,是需要在编译前要捕获的,如果不捕获,则不能通过编译。常见的编译时异常主要是IOException和ParseException
IOException:
ParseException
这个异常主要是解析时出现的异常。最有力的例子是String和Date互换时要捕获的。
<span style="font-size:18px;">public class NullPointExceptionDemo {
public static void main(String[] args) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = "2015-11-24";
try {
Date parseDate = df.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}</span>
以上是异常的分类以及常见的一些异常及特例,下一篇介绍一下异常的两种处理方法。