异常
异常简介
异常就是程序不正常。
异常的后果: 异常会导致程序非正常终止
异常的处理: 我们需要在程序出现异常的时候能够继续往下执行,这个时候我们就需要对异常做处理
异常的分类
常见的异常类型
异常处理
try-catch块
//我们使用try-catch对异常做处理,保证程序出现异常后不会终止,而会继续向下执行
try {
int i = 1/1;
System.out.println("------------------");
}catch(Exception e) {
System.out.println("有异常");
}
异常的常用方法
try {
int i = 1/0;
}catch(Exception e) {
//打印详细异常信息
e.printStackTrace();
System.out.println(e.getMessage());
}
finally
try-catch-finally
/**
* final finally finalize的区别
*/
try {
int i = 1/1;
}catch (Exception e) {
e.printStackTrace();
}finally {//最终的,不管try中有没有异常,最终总会执行
System.out.println("异常处理最终的代码");
}
System.out.println("***************************");
多重try-catch
//如果我们要对try中不同的异常,进行不同的处理我们要用到多重try-catch
try {
int i = 1/1;
// String str = null;
// System.out.println(str.length());
// ArrayList<String> arrayList = new ArrayList<String>();
// System.out.println(arrayList.get(5));
}catch (ArithmeticException e) {
System.out.println("出现了算术异常");
}catch (NullPointerException e) {
System.out.println("出现了空指针异常");
}catch (IndexOutOfBoundsException e) {
System.out.println("出现了下标越界异常");
}finally {
System.out.println("--------最终的--------");
}
System.out.println("=======================");
声明异常throws
/**
* throws写在方法的定义上
* 声明这个方法有异常,告诉方法的调用者对异常做处理
*/
public class Lesson6 {
public static void main(String[] args){
try {
fun2();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void fun2() throws Exception {
fun1();
}
/**
*
* @throws Exception
* 这个方法里面有异常,我不处理,把异常丢给调用者
*/
public static void fun1() throws Exception{
int i = 1/0;
}
}
throw
抛出一个具体的异常对象。
public static void main(String[] args) {
//throw 用来在方法内部抛出一个具体的异常对象
//1.throw抛非运行时异常
// try {
// throw new Exception("非运行时异常");
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//2.throw抛运行时异常
try {
throw new RuntimeException("运行时异常");
}catch (Exception e) {
e.printStackTrace();
}
}
自定义异常类
步骤:
a.建一个类
b.继承Exception类或者Exception的子类
c.根据父类生成构造方法生成本类的构造方法
自定义异常类的用法,使用throw抛出自定异常类对象
自定义非运行时异常类
public class MyExceptionOne extends Exception{
public MyExceptionOne() {
super();
// TODO Auto-generated constructor stub
}
public MyExceptionOne(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public MyExceptionOne(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public MyExceptionOne(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public MyExceptionOne(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
自定义运行时异常类
public class MyExceptionTwo extends RuntimeException{
public MyExceptionTwo() {
super();
// TODO Auto-generated constructor stub
}
public MyExceptionTwo(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public MyExceptionTwo(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public MyExceptionTwo(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public MyExceptionTwo(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
测试类:
public class Lesson1 {
public static void main(String[] args) {
try {
throw new MyExceptionOne("自定义非运行时异常类");
} catch (MyExceptionOne e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("-------------------");
try {
throw new MyExceptionTwo("自定义运行时异常类");
}catch (Exception e) {
e.printStackTrace();
}
}
}
案例
案例1
问题:要求对下面的代码进行升级,1.能够循环运算,2.出现异常程序不会终止。
import java.util.Scanner;
public class Lesson1 {
public static void main(String[] args) {
//要求对下面的代码进行升级,1.能够循环运算,2.出现异常程序不会终止
// 使用switch做一个两位数的+-*/的计算器
/*
* 1.接收用户输入第一个数 2.接收用户输入第二个数 3.接收用户输入运算符 4.以运算符为条件,使用switch实现功能
*/
Scanner input = new Scanner(System.in);
while(true) {
System.out.println("请输入第一个数");
int n1 = input.nextInt();
System.out.println("请输入第二个数");
int n2 = input.nextInt();
System.out.println("请输入运算符");
String symbol = input.next();
// 以运算符为条件,使用switch实现功能
switch (symbol) {
case "+":
System.out.println(n1+"+"+n2+"="+ (n1+n2));
break;
case "-":
System.out.println(n1+"-"+n2+"="+ (n1-n2));
break;
case "*":
System.out.println(n1+"*"+n2+"="+ (n1*n2));
break;
case "/":
System.out.println(n1+"/"+n2+"="+ (n1/n2));
break;
default:
System.out.println("运算符输出错误");
break;
}
}
}
}
答案:
package com.woniuxy.lesson1;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args) {
//要求对下面的代码进行升级,1.能够循环运算,2.出现异常程序不会终止
// 使用switch做一个两位数的+-*/的计算器
/*
* 1.接收用户输入第一个数 2.接收用户输入第二个数 3.接收用户输入运算符 4.以运算符为条件,使用switch实现功能
*/
Scanner input = null;
int n1 = 0;
int n2 = 0;
while(true) {
while(true) {
input = new Scanner(System.in);
try {
System.out.println("请输入第一个数");
//把可能会出现异常的代码放到try中
n1 = input.nextInt();
break;
}catch (Exception e) {
System.out.println("输入有误,请输入数字");
}
}
while(true) {
input = new Scanner(System.in);
try {
System.out.println("请输入第二个数");
//把可能会出现异常的代码放到try中
n2 = input.nextInt();
break;
}catch (Exception e) {
System.out.println("输入有误,请输入数字");
}
}
System.out.println("请输入运算符");
String symbol = input.next();
// 以运算符为条件,使用switch实现功能
switch (symbol) {
case "+":
System.out.println(n1+"+"+n2+"="+ (n1+n2));
break;
case "-":
System.out.println(n1+"-"+n2+"="+ (n1-n2));
break;
case "*":
System.out.println(n1+"*"+n2+"="+ (n1*n2));
break;
case "/":
System.out.println(n1+"/"+n2+"="+ (n1/n2));
break;
default:
System.out.println("运算符输出错误");
break;
}
}
}
}
案例2
老师正在上课,电脑蓝屏了或,电脑冒烟了。
模拟上面的异常,并对异常做处理,如果蓝屏了则重装系统,如果冒烟了则换电脑。
自定义蓝屏异常类
package com.woniuxy.lesson3;
/**
* 蓝屏异常类
* @author Administrator
*
*/
public class LanpingException extends Exception{
public LanpingException() {
super();
// TODO Auto-generated constructor stub
}
public LanpingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public LanpingException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public LanpingException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public LanpingException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
自定义冒烟异常类
package com.woniuxy.lesson3;
public class MaoyanException extends Exception{
public MaoyanException() {
super();
// TODO Auto-generated constructor stub
}
public MaoyanException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
public MaoyanException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public MaoyanException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public MaoyanException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
}
老师类
package com.woniuxy.lesson3;
public class Teacher {
/**
* 上课方法
* @param n
* @throws LanpingException蓝屏异常
* @throws MaoyanException冒烟异常
*/
public void teach(int n) throws LanpingException,MaoyanException{
System.out.println("老师正在上课......");
if(n == 1) {//模拟蓝屏异常
throw new LanpingException("电脑蓝屏了");
}else if(n == 2) {//模拟冒烟异常
throw new MaoyanException("电脑冒烟了");
}
}
}
测试类
package com.woniuxy.lesson3;
public class Test {
public static void main(String[] args) {
Teacher teacher = new Teacher();
/**
* 方法调用的时候出现的异常有2钟可能,我们使用多重try-catch块对异常分别处理
*/
try {
teacher.teach(2);
} catch (LanpingException e) {
e.printStackTrace();
System.out.println("重装系统");
} catch (MaoyanException e) {
e.printStackTrace();
System.out.println("换电脑");
}finally {
System.out.println("继续上课......");
}
}
}