异常:
从字面意思说:就是异与常太,和正常情况不一样,有错误出现。
阻止当前方法或作用域,称之为异常
java中的异常体系结构
Throwable(所有异常继承于它):
1.Error:虚拟机错误、线程死锁(一般很少接触)
2.Exception(编码、环境、用户操作输入出现问题):
①非检查异常,运行时异常(RuntimeException):空指针异常、数组下标越界异常、类型转换异常、算术异常,(一般是逻辑上错误)
②检查异常(CheckException):文件异常、SQL异常
在Java中用try-catch 和try-catch-finally捕获并处理异常
基本语法 :try{
//一些会抛出异常的方法,可能会发生异常的代码块
}catch(Exception e){//捕获异常
//处理该异常的代码块
}
如果try中发生异常,程序终止执行, 程序的控制权会交给catch块中的异常处理程序
try{
//一些会抛出异常的方法
}catch(Exception e){
//处理该异常的代码块
}catch(Exception2 e){
//处理Exception2的代码块
}……(n个catch块)……{
}finally{
//最终将要执行的一些代码
}
多重catch语句块应注意的问题:
①顺序问题。要按照先小后大、先子类后父类的顺序编写语句块(后面抛出的异常应包含前面抛出的异常)。
return在try-catch-finally中:
1、不管有木有出现异常,finally块中代码都会执行;
2、当try和catch中有return时,finally仍然会执行;
3、finally是在return后面的表达式运算后执行的(此时并没有返回运算后的值,而是先把要返回的值保存起来,管finally中的代码怎么样,返回的值都不会改变,任然是之前保存的值),所以函数返回值是在finally执行前确定的;
4、finally中最好不要包含return,否则程序会提前退出,返回值不是try或catch中保存的返回值。
JAVA中异常抛出
throw-----将产生的异常抛出(动作)
throws----声明将要抛出何种类型的异常(声明)
public void 方法名(参数列表)throws 异常列表{
//调用会抛出异常的方法,或者:throw new Exception();
}
JAVA中自定义异常
class 自定义异常类 extends 异常类型{
}
必须继承java异常类库中意思相近的异常类
Java中的异常链:
将捕获的异常包装成新的异常,然后在新的异常中添加对原始异常的引用,再把这个新的异常抛出。就像是链式反应一样,一个导致一个。
这个想法是指一个方法应该抛出定义在相同的抽象层次上的异常,(将所有捕获到的异常包装为新的异常类,即定义在相同的抽象层次上抛出)但不会丢弃更低层次的信息。
import java.util.InputMismatchException;
import java.util.Scanner;
public class my_try1 {
@SuppressWarnings("resource")
public static void main(String[] args) {
int age;
try{
System.out.println("请输入你的年龄:");
Scanner inpurt=new Scanner(System.in);
age=inpurt.nextInt();
System.out.println("十年后你"+(age+10));
}catch(InputMismatchException e){//输入不匹配异常
System.out.println("你应该输入整数!!!");
}
System.out.println("程序结束");//在异常处理段执行完毕后,程序顺序执行try-catch代码块的代码
}
}
import java.util.InputMismatchException;
import java.util.Scanner;
public class my_try2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
try{
System.out.println("请输入一个整数:");
int one=input.nextInt();
System.out.println("请输入第二个整数:");
int two=input.nextInt();
System.out.println("两个数相除的结果为:"+one/two);
}catch(InputMismatchException e){ //输入不匹配异常
System.out.println("你应该输入整数!");
}catch(ArithmeticException e){ //算术异常
System.out.println("除数不能为0");
}catch(Exception e){
System.out.println("我不知道的的异常");
}finally{
System.out.println("所有异常处理完毕,进行一些善后工作!!!");
} //用多个catch块捕获多种不同类型的异常
System.out.println("程序结束啦");
}
}
public class my_try3 {
public static void main(String[] args) {
my_try3 tct=new my_try3();
int result=tct.test();
System.out.println("test()方法,执行完毕,返回值为:"+result);;
int a=tct.test2();
System.out.println("我告诉你test2执行完毕"+a);
int resuly=tct.test3();
System.out.println("我告诉你test3执行完毕");
}
public int test(){
int divider=10;
int result=100;
try{
while(divider>-1){
divider--;
result=result+100/divider;
}
return result;
}catch(Exception e){
e.printStackTrace();//打印出异常的一些信息
System.out.println("循环抛出异常le!!!");
return -1;
}
}
public int test2(){
int divider=10;
int result=100;
try{
while(divider>-1){
divider--;
result=result+100/divider;
}
return result;
}catch(Exception e){
e.printStackTrace();//打印出异常的一些信息
System.out.println("循环抛出异常le!!!");
return result=999;
}finally{
System.out.println("这是finally!!哈哈");
System.out.println("我是Result!!!我的值是:"+result);
}
}
public int test3(){
int divider=10;
int result=100;
try{
while(divider>-1){
divider--;
result=result+100/divider;
}
}catch(Exception e){
e.printStackTrace();//打印出异常的一些信息
System.out.println("循环抛出异常le!!!");
}finally{
System.out.println("这是finally!!哈哈");
System.out.println("我是Result!!!我的值是:"+result);
}
System.out.println("我是test2我运行完毕!");
return 1111;
}
}
public class yc1 {
public static void divide(int one,int two) throws Exception{
if(two==0)
throw new Exception("两数相除,除数不能为零!!!");
else
System.out.println("两数相除结果为:"+one/two);
}
//方法一:在调用者 中接收处理异常
public static void compute(){
try{
divide(5,0);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
//方法二:向更高一级抛出异常
public static void compute2() throws Exception{
divide(5,0);
}
public static void main(String[] args) throws Exception {
//compute();
compute2();//会向调用着抛出异常
}
}
//自定义异常
public class yc2 extends Exception{
public yc2(){
}
public yc2(String message){
super(message);
}
}
//异常链
public class yc3 {
/*
* test1():抛出:喝大了异常
* test2():调用test1(),捕获“喝大了”异常,并包装成运行时异常,继续抛出
* main方法中,调用test2()尝试捕获test2()方法抛出的异常
*/
public void test1() throws yc2{
throw new yc2("开车别喝酒!");
}
public void test2(){
try {
test1();
} catch (yc2 e) {
//方法一:
//RuntimeException newExc=new RuntimeException("司机一滴酒,亲人两行泪~~");
//newExc.initCause(e);//传递产生的异常
//方法二:
RuntimeException newExc=new RuntimeException(e);//将捕获的异常传递进去
throw newExc; //抛出异常
}
}
public static void main(String[] args) {
yc3 ct=new yc3();
try{
ct.test2();
}catch(Exception e){
e.printStackTrace();
}
}
}