Java——异常
异常 : 程序生病了…
Throwable
/
Error Exception
/ /
Uncheck Checked Runtime
Exceiton Exceiton Exceiton
Error: 这类错误不用程序员解决
Exception:
CheckedException: 编译时异常|检查时异常
注意: 如果出现编译时异常,如果不做处理,程序无法执行
通过异常处理方式进行处理
RuntimeExceiton: 发生在程序运行期间
可以通过增强程序的健壮性处理(if),也可以通过异常处理方式进行处理
注意: 如果程序出现异常,不处理程序无法继续执行
常见的运行时异常:
NullPointerException 运行时异常
NegativeArraySizeException 数组长度负值异常
ArrayIndexOutOfBoundsException 数组索引越界异常
ArithmeticException 数学异常
NumberFormatException 数字转换异常
public class ExceptionDemo01 {
public static void main(String[] args) {
//编译时异常
//InputStream is=new FileInputStream("");
//运行时异常
//NullPointerException
String str=null;
//通过增强程序的健壮性处理
if(str!=null) {
System.out.println(str.length());
}
//数组
//NegativeArraySizeException
/*
* int[] arr=new int[5]; System.out.println(arr[5]);
*/
//ArithmeticException
//System.out.println(6/0);
//NumberFormatException
String s="1233a";
//System.out.println(Integer.valueOf(s)+123); //字符串转为数字
}
}
异常处理方式:
- 1.throws 异常抛出 异常抛出到上一层,谁用谁处理
- 2.try…catch 异常捕获
-
try { 可能会出现异常的代码 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (Exception e){ 出现异常可能会执行的代码 } finally{ 无论try中的代码是否出现异常,都会执行finally中的代码 (一般为资源的关闭..) }
一旦try中的代码出现异常了,下面代码不会执行,直接执行对应的catch
一个try后面可以跟多个catch,范围大的catch放后面
public class ExceptionDemo02 {
public static void main(String[] args) {
try {
System.out.println("try开始啦");
test();
System.out.println(5/0);
//System.out.println("".length());
System.out.println("try结束了");
} catch (FileNotFoundException e) {
// e.printStackTrace();
System.out.println("文件未找到...");
} catch (NullPointerException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
System.out.println("无论程序是否出现异常,都会执行的代码");
}
System.out.println("main方法继续执行.....");
}
static void test() throws FileNotFoundException {
InputStream is=new FileInputStream("D:/heihei.txt");
}
}
自定义异常
- 如何自己创造异常: throw 制造异常对象
- 如何自定义异常类
- 先阶段使用的异常类都是java提供的
- 异常类也是类,直接或者间接的继承自Exception
- 如果继承自RuntimeException或它的子类,当前的类就是运行时异常
- 如果直接继承自Exception或者其他的编译时异常,当前的类就是编译时异常
public class DefinedException03 {
public static void main(String[] args) {
Person p=new Person("zhangsan",13);
p.setName("zhangsanfeng");
try {
p.setAge(-19);
} catch (AgeException e) {
e.printStackTrace();
}
System.out.println(p);
System.out.println(123);
}
static void test() throws FileNotFoundException {
//new DefinedException03();
//throw new FileNotFoundException(); //普通对象
//InputStream is=new FileInputStream("D:/heihei.txt");
}
}
//自定义异常
class AgeException extends Exception{
public AgeException() {
// TODO Auto-generated constructor stub
}
public AgeException(String message) {
super(message);
}
}
class Person{
private String name;
private int age;
public Person() {
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException{
if(age<0 || age>150) {
//System.out.println("年龄不合法");
throw new AgeException(age+"不合法");
//return;
}
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}