黑马程序员技术交流社区
标题:
常见的异常类型(总结)
[打印本页]
作者:
上篮高手
时间:
2013-10-19 15:41
标题:
常见的异常类型(总结)
1: 算数异常 ArithmeticException
除0 导致的异常
package com.mingrisoft.exceptions;
public class ExceptionTest {
public static void main(String[] args) {
System.out.println("-1.0 / 0 = " + (-1.0 / 0));// 演示负浮点数除0
System.out.println("+1.0 / 0 = " + (+1.0 / 0));// 演示正浮点数除0
System.out.println("-1 / 0 = " + (-1 / 0));// 演示负整数除0
System.out.println("+1 / 0 = " + (-1 / 0));// 演示正整数除0
}
}
复制代码
错误:Exception in thread "main" java.lang.ArithmeticException: / by zero
2: 数组存值异常 ArrayStoreException
在String 数组中存入了 Integer 对象
package com.mingrisoft.exceptions;
public class ExceptionTest {
public static void main(String[] args) {
Object array[] = new String[3]; // 声明一个长度为3的Object类型的数组
array[0] = new Integer(1); // 将数组的第一个元素赋值为整数对象1
System.out.println(array[0]); // 输出数组的第一个元素
}
}
复制代码
错误: Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
3: 数组下标越界异常 ArrayIndexOutOfBoundsException
数组length为 5, 遍历的时候到了 5
package com.mingrisoft.exceptions;
import java.util.Arrays;
public class ExceptionTest {
public static void main(String[] args) {
int[] array = new int[5]; // 声明一个长度为5的整型数组
Arrays.fill(array, 8); // 将新声明的数组所有元素赋值为8
for (int i = 0; i < 6; i++) {// 遍历输出所有数组元素
System.out.println("array[" + i + "] = " + array[i]);
}
}
}
复制代码
错误: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
4: 空指针异常 NullPointerException
string 对象为 null
调用 它的函数会导致空指针异常。
package com.mingrisoft.exceptions;
public class ExceptionTest {
@SuppressWarnings("null")
public static void main(String[] args) {
String string = null;// 将字符串设置为null
System.out.println(string.toLowerCase());// 将字符串转换成小写
}
}
复制代码
错误: Exception in thread "main" java.lang.NullPointerException
5: 类未发现异常 ClassNotFoundException
由于我本机并没有
com.mysql.jdbc.Driver
package com.mingrisoft.exceptions;
public class ExceptionTest {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");// 加载MySQL驱动程序
} catch (ClassNotFoundException e) {// 捕获异常
e.printStackTrace();// 打印堆栈信息
}
}
}
复制代码
错误: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
6: 非法访问异常 IllegalAccessException
这个例子是涉及到反射技术。
试图要访问String 这个类 中的hash 成员变量。 hash 变量为private
package com.mingrisoft.exceptions;
import java.lang.reflect.Field;
public class ExceptionTest {
public static void main(String[] args) {
Class<?> clazz = String.class;// 获得代表String类的类对象
Field[] fields = clazz.getDeclaredFields();// 获得String类的所有域
for (Field field : fields) {// 遍历所有域
if (field.getName().equals("hash")) {// 如果域的名字是hash
try {
System.out.println(field.getInt("hash"));// 输出hash的值
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
复制代码
错误: java.lang.IllegalAccessException: Class com.mingrisoft.exceptions.ExceptionTest can not access a member of class java.lang.String with modifiers "private"
7: 文件未发现异常: FileNotFoundException
我的机子并没有这个文件
d:\\kira.txt"
package com.mingrisoft.exceptions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ExceptionTest {
public static void main(String[] args) {
FileInputStream fis = null;// 创建一个文件输入流对象
try {
File file = new File("d:\\kira.txt");// 创建一个文件对象
fis = new FileInputStream(file);// 初始化文件输入流对象
} catch (FileNotFoundException e) {// 捕获异常
e.printStackTrace();
} finally {
try {
fis.close();// 释放资源
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
复制代码
错误: java.io.FileNotFoundException: d:\kira.txt (系统找不到指定的文件。)
8: 还有好多异常 待补充
作者:
murder_fol
时间:
2013-10-19 22:16
总结不错
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2