异常
Java异常的概念
广义的异常:
(1)错误(Error):程序运行时出现的错误,程序无法解决。
(2)狭义的异常(Exception):程序在运行时出现不正常的情况,可以通过异常处理机制来处理的异常。
常见异常类型:
ArrayIndexOutOfBoundsException:数组下标越界异常
出现条件:使用超出数组下标范围的下标
ArithmeticException:算术异常
出现条件:涉及到数学运算的地方可能出现错误,例如程序中出现分母为零
NullPointerException:空指针异常
出现条件:当使用的对象值为null
ClassCastException:类型转换异常
出现条件:如进行向下转型时,转换对象无法完成正常转换
public class Demo1 {
public static void main(String[] args) {
/*int [] a = new int[6];
System.out.println(a[6]); //数组越界异常 java.lang.ArrayIndexOutOfBoundsException*/
/*int a = 10;
int b = 0;
System.out.println(a/b); //算数异常 java.lang.ArithmeticException*/
Object obj = "123";
Integer a = (Integer) obj;
System.out.println(a); //类型转换异常 java.lang.ClassCastException
/* String s = null;
System.out.println(s.length()); //空指针异常,使用的对象值为null关键字 java.lang.NullPointerException*/
}
}
异常的体系
异常处理
try{
编写可能出现异常的代码;
}catch(捕获的异常类型 形参){
异常处理 提示 做出其他操作
}
多个catch
`package com.ffyc.javaexception;
public class Demo3 {
public static void main(String[] args) {
/*try{
int a = 10;
int b = 0;
int c = a/b;
String s = null;
System.out.println(s.length());
int []d = new int[3];
d[3] = 2;
}catch (ArithmeticException e){
System.out.println("算数异常");
}catch (NullPointerException n){
System.out.println("空指针异常");
}catch (Exception ex){
System.out.println("系统忙,请稍后再试");
}
}*/
try {
int a = 10;
int b = 0;
int c = a / b;
try {
String s = null;
System.out.println(s.length());
} catch (NullPointerException n) {
System.out.println("空指针异常");
}
}catch (ArithmeticException e) {
System.out.println("算数异常");
}
}
}
try-catch-finally
public class Demo4 {
public static void main(String[] args) {
try {
int a = 10;
int b = 0;
int c = a / b;
}finally {
System.out.println("finally"); //出现异常但未处理,程序不能继续执行,但finally中的代码可以执行
}
System.out.println("后面的代码");
}
}
public class Demo5 {
public static void main(String[] args) {
test();
}
public static int test(){
try {
int a = 10;
int b = 0;
int c = a / b;
return c;
}catch (ArithmeticException e) {
System.out.println("算数异常");
return -1;
}finally { //即使出现异常,finally总会执行
System.out.println("最终执行的代码");
}
}
}
throws关键字
throws:声明此方法可能会出现异常,表示将异常抛出,不处理异常。
任何方法都可以使用throws关键字声明异常类型,包括抽象方法。
import java.io.UnsupportedEncodingException;
public class Demo6 {
public static void main(String[] args) {
try {
Demo6.test1();
} catch (UnsupportedEncodingException e) {
System.out.println("编码错误");
}
}
/*声明test1()可能会出现异常*/
public static void test1() throws UnsupportedEncodingException {
Demo6.test2();
}
/*模拟一个偏底层的方法,这样一般不try{}catch(){}
*继续声明此方法可能会出现异常,作为方法的声明(定义)
*/
public static void test2() throws UnsupportedEncodingException {
String s = "123";
s.getBytes("utf-8");
}
/*throws UnsupportedEncodingException 编译期异常,提示方法调用的地方,在编译时期就要做出处理
*throws NullPointerException 运行期异常,在编译期间不显示的提示处理
*/
}
throw关键字
throw:在方法中直接抛出一个异常类的对象,方法终止执行。
new+有参构造方法(描述异常的原因)
import java.io.UnsupportedEncodingException;
public class Demo7 {
public static void main(String[] args) {
try {
trans(101);
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); //在控制台打印异常信息,在开发测试期间使用
System.out.println(e.getMessage());
}
}
public static String trans(int score) throws UnsupportedEncodingException {
if(score<0||score>100){
throw new UnsupportedEncodingException("非法分数");
}
if(score>90){
return "A";
}
else {
return "B";
}
}
}
自定义异常类
在业务开发过程中,根据实际业务需求,自己开发的一个异常类来表示某一类问题。
public class ScoreExceptiong extends Exception{
/*
分数不合法抛出此类的对象 例如 百分制试卷 传入一个小于0 或大于100的分数
自定义异常: 我们在业务开发过程中,可以根据实际业务需求自己开发一个异常类,表示某一类问题
*/
public ScoreExceptiong(){
super();
}
public ScoreExceptiong(String message){
super(message);
}
}