09-06-06 Exception
1.Throwable(运行时可能出现的任何问题的总称)
1)Error(非常严重的错误,系统不要求程序员处理)
2)Exception(从代码的角度看是程序员可以处理的问题;异常,例外,运行的的概念)
a. UncheckedException 未检查异常(RuntimeException)
此类异常程序员小心谨慎完全可以避免的异常,系统不要求程序员处理。
eg:3/0;NullPointerException;类型转换异常;数组下标越界异常。
b.CheckedException(已检查异常)系统要求必须处理。
好像地震是人无法预测的,但是国家对建筑材料的指标有明确规定。
又如打雷无法避免,但可以在建筑物顶上安放避雷针。
2.异常处理的方式
1)声明抛弃异常(消极的处理):
关键字throws eg:
public class TestException(
public static void main(String[] args){
TestException te=new TestException();
System.out.println("this is main begin");
m1();
System.out.println("this is main end");
}
public void m1(){
System.out.println("this is m1 begin");
m2();
System.out.println("this is m1 end");
}
public void m2(){
System.out.println("this is m2 begin");
int num=3/0;
System.out.println("this is m2 end");
}
)
如果程序如此则三条显示结束的语句都不能输出,有两种方法处理
第一种声明抛弃 类定义语句分别改为:
public class TestException throws ArithmeticException
public void m1() throws ArithmeticException
public void m2()throws ArithmeticException//数学异常,所有的异常都是Exception的子类,api
//long包下的Exceptionj里可以看到所有异常类型。
//上面为未检查异常,即使不处理,编译也不会报错。下面为已检查异常:
import java.io.*;
public class TestException throws FileNotFoundException(
public static void main(String[] args){
TestException te=new TestException();
System.out.println("this is main begin");
te.m1();
System.out.println("this is main end");
}
public void m1()throws FileNotFoundException{
System.out.println("this is m1 begin");
m2();
System.out.println("this is m1 end");
}
public void m2() throws FileNotFoundException{
System.out.println("this is m2 begin");
//int num=3/0;若该行有效,则该段代码里会出现两种异常,上面的抛出项里要再添上数学异常。
FileInputStream fis=new FileInputStream("1.txt");//若找不到文件,异常就会产生。
System.out.println("this is m2 end");
}
)
异常类型有很多,所以最好的处理方法是,不管哪行代码出异常都向外抛弃(一个方法可以抛出多个异常)。
为省事也可以把异常类型扩大,可以都写为Exception,但是要注意,一旦扩大,上层就不可以再缩小。
关键字throws要写在方法名的定义上。
当代码出现异常时,,代码不会向下执行,Jvm会将异常封装成相应的异常类对象,向外抛出
throw:"抛出"
eg: throw new FileNotFoundException();//此行代码可以由程序员写在方法的实现里,用来抛出异常对象
throw后面跟的是异常对象,即程序员模拟Jvm的动作。抛出语句写在实现里,后面的代码就没有机会再
执行。
在此重申方法的返回值:1.正常运行,要求方法必须返回定义的类型的值。2.非正常结束,返回的是异常对
象。eg:
public int m3(){//调用该方法返回的就是异常对象,故编译时不会出错。
int num=3/0;
return num;
}
****方法覆盖里,子类不可以比父类抛出的异常范围大(假设父类里是FileNotFoundException
则子类里不可以写成Exception)。
2)try-catch (积极的处理):
try(
1xxxxxx;
2xxxxxxx;
3xxxxxxx;
)catch(异常对象1){
4xxxxxxx;
}catch(异常对象2){
5XXXXX;
}finally(//不管出异常与否都要执行的语句;
7xxxxxx;
)
6xxxxxx;
第一种,如果1,2,3没产生任何异常,3执行后直接执行5;
第二种,如果执行到2产生异常,3不会被执行,直接跳出try{},匹配第一个catch,和catch里定的类型一致,
执行语句4,随后执行语句6;如果不一致,再匹配第二个catch,随后执行5,然后执行6;以次类推;若
都不匹配,则语法出问题。eg:
public class TestException(
public static void main(String[] args){
TestException te=new TestException();
System.out.println("this is main begin");
try{
te.m1();
int num=te.m3();
ti.m4();
}catch(ArithmeticException e){
e.printStackTrace();//打印异常堆栈信息;
}catch(FileNotFoundException fe){//一个try可以匹配多个catch
fe.printStackTrace();//打印异常堆栈信息;
}catch(IOException ie){//写catch子类先写,父类后写,即该catch不可以写在前两个之前。
ie.printStackTrace();//打印异常堆栈信息;
}finally(
)
System.out.println("this is main end");
}
public void m1(){
System.out.println("this is m1 begin");
m2();
System.out.println("this is m1 end");
}
public void m2() throws FileNotFoundException{
System.out.println("this is m2 begin");
FileInputStream fis=new FileInputStream("1.txt");//若找不到文件,异常就会产生。
System.out.println("this is m2 end");
}
public int m3() throws ArithmeticException{
int num=3/0;
return num;
}
public void m4() throws IOException(
throw new IOException();
)
)//TestException
//看一下语句体finally
public class TestFinally{
public static void main(String[] args){
System.out.println(getNum(1));
System.out.println(getNum(0));
System.our.println(getNum(2));
}
public static int getNum(int num){
try{
int k=2/num;
return 1;
}catch(Exception e){
return 0;
}finally{
if(num==2)return 2;
}
}
}//显示结果1,0,2
方法里面,return语句也无法阻档finally执行,但当try catch里出现了System.exit(0),finally
不会再执行。
3.自定义异常类,通常叫作业务异常
eg:
class MyException extends Exception{
//已检查异常,若想写成未检查异常,则继承RuntimeException
}
最后写一个恶搞的小段代码
//排值日表,返回值日生名称,只要是wang,就抛异常。
import java.util.*;
public class TestMyException{
public static void main(String[] args){
Scanner inn=new Scanner(System.in);
String name=inn.nextString();
getStudnent(name);
}
public static void getStudent(){
if(name.epuals("wang")){
throw new StudentIll("gan mao");
}else(
System.our.println(name);
)
}
}
先来写一个自定义的异常类
class StudentIll extends Exception{
public StudentIll(String str){//带参的构造方法
super(str);//借助父类将传入的错误信息传入椎栈里
}
}
java异常
最新推荐文章于 2024-09-14 18:46:05 发布