Exception异常
java异常定义:
try{
copyFile("d:/a.txx","e:/e.txt");
}catch (Exception e){
e.printStackTrace();
}
异常处理方式:
1,抛出异常:执行方法出现异常,则交给改异常的对象,停止当前执行路径,j交给java运行环境即JRE
2,补货异常,JRE获取异常后,寻找相对应的代码处理异常,再方法调用栈中查找异常,从生成的异常方法开始会输,知道找到异常处理代码为止,
分类
根类为Throwable,子类为Error下面为不检查异常,即unchecked,一般为服务崩溃,重启即可,另外一个为Exception,分为检查异常checked和运行时异常Runtime。
Exception:
-
RuntimeException :运行时异常
-
CheckedException:已检查异常
常见的运行时异常/**
- 运行时异常
*/
public class tryFile {
public static void main(String[] args) {
int a=1;
int b=0;
//ArithmeticException
if(b!=0){
System.out.println(a/b);
}
//NullPointerException
String str=null;
if(str!=null){
str.length();
}
//ClassCastException
Animal dog=new Dog();
if(dog instanceof Cat){
Cat c=(Cat)dog;
}
//ArrayIndexOutOfBoundsException
int[] arr=new int[5];
System.out.println(arr[3]);
//NumberFormatException
String str1=“999a”;
System.out.println(Integer.valueOf(str1));
}
}
class Animal{
}
class Dog extends Animal{
}
class Cat extends Animal{
}
- 运行时异常
已检查异常:CheckedException 编译时报错,即所有非运行时异常,一般处理为try catch finally。
try:它里面放置可能引发异常的代码
catch:后面对应异常类型和一个代码块,用于表明该catch块用于处理这种类型的代码块,可以有多个catch块。
finally:主要用于回收在try块里打开的物力资源(如数据库连接、网络连接和磁盘文件),异常机制总是保证finally块总是被执行。只有finally块,执行完成之后,才会回来执行try或者catch块中的return或者throw语句,如果finally中使用了return或者 throw等终止方法的语句,则就不会跳回执行,直接停止
java的垃圾回收机制不会回收任何的物理资源,垃圾回收机制只回收堆内存中对象所占用的内存。
try catch示例
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class CheckedFile {
public static void main(String[] args) {
FileReader fr=null;
try {
fr=new FileReader("d:/a.txt");
System.out.println("strp1");
char c1=(char)fr.read();
System.out.println(c1);
} catch (FileNotFoundException e) {
//将该异常的跟踪栈信息输出到标准错误输出
//子类异常一般早父类之前
System.out.println("strp2");
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally {
System.out.println("strp3");
try {
if(fr!=null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
跑出异常二:
import java.io.FileReader;
import java.io.IOException;
public class TestChecked3 {
public static void main(String[] args) throws IOException {
reandMyFile();
}
public static void reandMyFile() throws IOException {
FileReader fr=null;
fr=new FileReader("d:/a.txt");
System.out.println("strp1");
char c1=(char)fr.read();
System.out.println(c1);
if(fr!=null){
fr.close();
}
}
}
自定义异常:RuntimeException,自定义运行时异常:
public class TestException {
public static void main(String[] args) {
Person p=new Person();
p.setAge(-10);
}
}
class Person{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
if(age<0){
throw new IllegaException("年龄不能为负数");
}
this.age = age;
}
}
class IllegaException extends RuntimeException {
public IllegaException(){
}
public IllegaException(String msg){
super(msg);
}
}
编译时异常:继承Exception,需要try catch
public class TestException {
public static void main(String[] args) {
Person p=new Person();
p.setAge(-10);
}
}
class Person{
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
if(age<0){
try{
throw new IllegaException("年龄不能为负数");
}catch(IllegaException e){
e.printStackTrace();
}
}
this.age = age;
}
}
class IllegaException extends Exception {
public IllegaException(){
}
public IllegaException(String msg){
super(msg);
}
}