java---异常处理

异常的概念

所谓错误,是指在程序运行过程中发生的一些异常事件

设计良好的程序,应该在异常发生时提供处理这些错误的方法,好的程序不会因为异常的发生而阻断或产生不可预见的结果
Java程序的运行过程中,如果出现异常事件,可以生成一个异常对象,该异常对象封装了异常事件的信息
当Java运行时系统接收到异常对象时,会寻找能处理这一异常的代码并把当前异常对象交给其处理,这一过程称为捕获(catch)异常

异常捕获(try-catch-finally)
try语句的作用是用于捕获可能发生异常的代码块;在try的后面会跟一个或多个catch语句块,在catch语句块中主要是编写异常的处理方法;finally语句块是无论发生异常与否都会执行的语句块


自定义异常类的作用与好处

自定义异常类的功能就是:如果开发人员需要自己开发实现一些规则,如果情况不能满足自己的要求,就需要向外抛出自己的异常。
package randException;


public class MyException extends ExceptionInInitializerError {
public MyException(){}
public MyException(String msg){
//异常的提示信息
System.out.println(msg);
System.out.println("考试分数只能在0-100之间");
}
}



package randException;


import java.util.Scanner;


import org.junit.Test;




public class jTest {
@Test
public void test1(){
try {
System.out.println("请输入分数");
double score=new Scanner(System.in).nextDouble();
checkScore(score);
} catch (MyException e) {
e.printStackTrace();
}
}
private void checkScore(double score) throws MyException{
if(score<0||score>100){
throw new MyException("分数不正确");
}else{
System.out.println("您在这次考试中的得分是:"+score);
}
}
}


异常树:
throwable:
1.Error  (java虚拟机异常程序员不作处理)                       


2. Exception    (程序员可捕获的所有异常的父类)
  IOException        RuntimeException
  输入/输出流异常      运行时异常

有如下两个变量定义:


1 int[] zero = new int[0];
2 int[] nil = null; 


这两种定义有什么区别呢?


 zero是一个长度为0的数组,我们称之为“空数组”,空数组也是一个对象,只是包含元素个数为0。


 nil是一个数组类型的空引用。


 假设一个方法返回一个数组,如果它返回null,则调用方法必须先判断是否返回null,才能对放回数组进一步处理,而如果返回空数组,则无须null引用检查。鉴于此,返回数组的方法在没有结果时我们通常返回空数组,而不是null,这样做对于函数调用者的处理比较方便。


arr=null是对的,因为arr是一个对象,所以可以置空。




代码如下:

package Exception;


import java.util.Arrays;
import java.util.Scanner;


import org.junit.Test;


public class Jtest {
@Test
public void test(){
int[] arr={1,2,3,5,4,6};//测试的数组下标越界异常
//String[] arr=null;//测试的空指针异常
//int k;//测试的数学异常
try{//将可能发生异常的代码块包裹起来
System.out.println(arr[9]);
//k=10/0;
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("数组下标越界异常");//捕获到了数组下标越界异常
e.printStackTrace();
}catch(NullPointerException e){
System.out.println("捕获到了空指针异常");//将发生异常的信息进行处理  捕获到了空指针异常
e.printStackTrace();
}catch(ArithmeticException e){
System.out.println("数学异常");
e.printStackTrace();
}finally{
System.out.println("finish");
}
}
@Test
public void test1(){
Scanner sc=new Scanner(System.in);
String[] cart=null;
while(true){
String goods=sc.next();
cart=Arrays.copyOf(cart,cart.length+1);
cart[cart.length-1]=goods;
System.out.println("购物车的商品是:"+Arrays.toString(cart));
}
}
@Test
public void test2(){
//String[] cart=null;
String[] cart={"ds"};
try{//将可能发生异常的代码块包裹起来
System.out.println(cart[0]);
}catch(Exception e){//将发生异常的信息进行处理
System.out.println("捕获到了异常");
e.printStackTrace();//输出异常信息
cart=new String[0];

}finally{//无论程序是否出现异常,finally语句都会执行
System.out.println("必须要执行");
//文件的关闭close
//数据库的关闭(增删改查)
}
System.out.println("finish");
}
@Test
public void test3(){
String str="helloworld";
System.out.println(Integer.parseInt(str));//数据类型转换错误
}
@Test
public void test4(){
String str="12345";
System.out.println(Integer.parseInt(str)+2);
}
@Test
public void test5(){
String str="null";
try {
System.out.println(Integer.parseInt(str)+2);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
@Test
public void test6(){
String str="null";
if(!("null".equals(str))){
System.out.println(Integer.parseInt(str)+2);
}

}

@Test
public void test7(){
String[] cart=null;
if(cart!=null){
try {// 将可能发生异常的代码块包裹起来
System.out.println(cart[0]);
} catch (NullPointerException e) {// 将发生异常的信息进行处理
System.out.println("捕获到了异常");
e.printStackTrace();// 输出异常信息
cart = new String[0];


}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("get exception");
e.printStackTrace();
}finally {// 无论程序是否出现异常,finally语句都会执行
System.out.println("必须要执行");
// 文件的关闭close
// 数据库的关闭(增删改查)
}
System.out.println("finish");
}
try {
test8();
} catch (NullPointerException e) {
e.printStackTrace();
}

}
@Test
public void test8() throws NullPointerException{//标识
ee();
}
private void ee(){
int[] arr=null;
if(arr==null){
throw new NullPointerException();
}
System.out.println(arr[0]);
}
//空指针异常-NullPointerException
@Test
public void test9(){
int[] arr=null;
System.out.println(arr[0]);
}

//数组下标越界异常-IndexOutBoundsException
@Test
public void test10(){
int[] arr={1,2,3,4,5,6};
System.out.println(arr[6]);
}
//类型转换异常-NumberFormatException
@Test
public void test11(){
String hello="hello";
System.out.println(Integer.parseInt(hello));
}
@Test
public void test12(){
int i=0;
try {//捕获异常
i=10/0;
} catch (Exception e) {//处理捕获到的异常
i=0;//如果捕获到了异常,把i的值赋值为0
e.printStackTrace();//打印详细的异常信息
}finally{//无论代码是否发生异常,都要执行
i++;
}
System.out.println(i);
}

@Test
public void test13(){
int[] arr=null;
try {
System.out.println(arr[0]);
} catch (NullPointerException e) {
e.printStackTrace();
arr=new int[1];
arr[0]=10;
}catch(ArithmeticException e){
e.printStackTrace();
arr[0]=0;
}finally{
arr[0]++;
}
System.out.println(arr[0]);
}
@Test
public void test14(){
int result=0;
try {//试图捕获异常
result=getResult(10,0);
} catch (ArithmeticException e) {//定义处理异常的代码
e.printStackTrace();//打印捕获的异常信息
result=0;
}
System.out.println(result);
}
//通过throws关键字抛出异常信息,表示该异常本方法不处理,交给调用者处理
private int getResult(int a,int b) throws ArithmeticException{
if(b==0){
throw new ArithmeticException("除数不能是0");//抛出异常对象
}
return a/b;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值