1.异常
异常的知识点不好理解,要求同学们学习异常,主要的目的记住使用格式
1.1 try…catch异常处理
try catch的异常处理的格式写法:
try{
被检测的代码
可能发生异常的代码
}catch(异常类的类名变量名){
异常的处理方式:写什么都可以
定义变量,创建对象,调用方法,循环,判断…
只要写了catch,异常就被处理掉了I
}
package com.sdjzu.exception;
public class ExceptionTest {
public static void main(String[] args) {
exception();
}
public static void exception(){
int[] str={1,2};
str[3]=3;
System.out.println("str = " + str);
System.out.println(1111);
}
}
package com.sdjzu.exception;
public class ExceptionTest {
public static void main(String[] args) {
exception();
}
public static void exception(){
int[] str={1,2};
try{
str[3]=3;
System.out.println("str = " + str);
}catch(Exception ex){
System.out.println("异常被处理");
}
System.out.println(1111);
}
}
打印异常信息
package com.sdjzu.exception;
public class ExceptionTest {
public static void main(String[] args) {
exception();
}
public static void exception(){
int[] str={1,2};
try{
str[3]=3;
System.out.println("str = " + str);
}catch(Exception ex){
System.out.println("异常被处理");
/*
* Throwable类的异常信息的方法
* */
String s1=ex.getMessage();
System.out.println("s1 = " + s1);
String s2=ex.toString();
System.out.println("s2 = " + s2);
ex.printStackTrace();
}
System.out.println(1111);
}
}
1.2多catch并行处理
I
异常处理的代码中: try可以跟随多个catch
好处:不同的异常,可以区别对待.分开处理
package com.sdjzu.somecatch;
public class SomecatchTest {
/*
* 多catch异常
* */
public static void main(String[] args) {
try {
exc(1);
}catch (NullPointerException ex){
System.out.println("控制针异常被处理");
}catch (ArrayIndexOutOfBoundsException ex){
System.out.println("越界异常被处理");
}
}
public static void exc(int i){
//空指针异常
if(i==0){
String[] str=null;
System.out.println("str.length = " + str.length);
}
//数组越界异常
if(i==1){
int[] str={1,2};
str[4]=4;
}
}
}
多个catch处理异常的时候写法特别注意:如果catch中的异常类没有关系,先写
后写没有区别,catch中的异常类有继承关系.父类写在最下面
1.3 throw和throws关键字的使用
●throw关键字:只能写在方法内部,关键字的后面跟随对象的创建
●throws关键字:只能写在方法的定义上,关键字后面跟随异常类名
package com.sdjzu.throw1;
public class Exception {
public static void main(String[] args) {
int length=-3;
try {
System.out.println("getArea(length) = " + getArea(length));
}catch (java.lang.Exception e){
e.printStackTrace();
}
}
//计算正方形的面积
public static int getArea(int length) throws java.lang.Exception {
if(length<=0){
throw new java.lang.Exception("参数不合法");
}
return length*length;
}
}
1.4 finally代码块
finally代码块跟随try … catch使用.也有跟随try使用
finally代码块里面的程序,无论是否出现异常,都会执行,必须执行
结束JVM了,finally不执行.
主要用于释放资源
package com.sdjzu;
/*
* finally代码块
* */
public class FinallyTest {
public static void main(String[] args) {
method();
}
public static void method(){
//异常的处理
try{
int[] arr={2};
System.out.println("arr[1] = " + arr[1]);
}catch (Exception e){
e.printStackTrace();
}
//无论出不出现异常finally代码块都要运行
finally{
System.out.println("无论出不出现异常finally代码块都要运行");
//不要再finally中return
//return 1;
}
}
}
1.5 RuntimeException异常
异常的父类是Exception,Exception类的子类RuntimeException,凡是
RuntimeException和他的所有子类都称为运行异常,非子类的称为编译异常
●编译异常:方法出现编译异常,调用者必须处理否则编译失败.处理方式可以是try
catch或者是throws都可以
●运行异常:方法出现运行异常,方法的定义上不需要throws声明,调用者也不需要
处理这个异常
不要处理运行异常:程序- -旦发生运行异常,请程序人员修改源码
●常见的运行异常
。Nu11PointerException 空指针
。IndexoutofBoundsException 越界异常
。ClassCastException 类型强制
。Il1ega1ArgumentException 无效的参数异常
1.6自定义异常.
Java官方已经定义了大量的异常类,但是依然不够,以后做项目的时候会出现的异
常在DK中没有定义的需要我们自己定义异常
●自定义异常,入伙,继承Exceptipn或者RuntimeException
。只有Exception和他的子类才具有可抛出性
●自定义的类中构造方法super调用父类构造方法,传递异常信息
package com.sdjzu.difinedExceptionClass;
/*
* 自定义异常类
* */
public class ScoreException extends RuntimeException{
//显示异常信息
//自定义类的构造器进行传参,调用父类的构造器
public ScoreException(String s){
super(s);
}
}
package com.sdjzu.difinedExceptionClass;
public class ExceptionTest {
public static void main(String[] args) {
getScore(100,-2);
}
//求平均值的方法
public static int getScore(int math,int Chinese){
//数据的安全效验
if(math<0||Chinese<0){
//抛出自定义异常
throw new ScoreException("成绩输入错误");
}
return (math+Chinese)/2;
}
}