面向对象中的异常(1)
一、异常和错误
1.异常是什么
java运行期间发生的问题就是异常。
2.异常和错误
异常是程序在运行期间发生了异常,通常有针对性的处理方式。
错误是程序在运行期间发生了错误,通常不会有针对性的处理方式。而这种问题一般是系统级别的,无法针对处理,只能修正代码。
3.异常代码
public class Demo{
public static void main(String[] args){
System.out.println(3/0);//该句中出现了0作为被除数,代码出现异常,无法继续执行。
}
}
二、异常的应用
1.异常在代码中的用法和作用
在编写程序的时候,需要考虑到程序的问题情况,即在程序中,功能需要接收参数,这时需要对参数进行是否合法的判断,如果数据不合法,需要通过异常的方式告诉调用者,即该功能是考虑到程序的健壮性。
2.应用代码
public class Demo{
public static void main(String[] args) {
Student s = new Student("小徐",120);//这里传输的年龄参数有误,程序直接在此停止
System.out.println(s);
}
}
public class Student {
private String name;
private int age;
Student(String name,int age){
//这里设置了年龄参数的范围
if (age<0||age>100){
//当程序出现异常的时候,会在这里报错。
throw new IllegalArgumentException("输入的年龄不合法");
}
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.异常体系结构图
1、Exception异常
在函数内抛出Exception,编译失败,因为编译器在检查语法时发生了错误。该程序已经出现问题,java认为这个程序本身存在隐患,需要捕获或者声明出来。
2、RuntimeException异常
不是功能本身发生的异常,而是因为比如调用者传递参数错误而导致功能运行失败。这也是问题,需要通过异常来体现,但是这个异常不要声明出来的。
同时异常分编译时异常和运行时异常两种,编译时的异常是编译器会检测的异常,而运行时异常是编译器不会检测的异常。不需要声明。声明也可以,如果声明了,无外乎就是让调用者给出处理方式。
4.自定义异常
异常是将问题封装成了对象,但系统发出的异常不好认,所以我们可以模拟java中的机制,我们自定义异常的信息和异常的名字,可以让我们准确对程序中的问题进行描述。
自定义异常会产生的问题和解决办法
自定义异常被抛出,需要继承Throwable,或是继承Throwable的子类,只有这样该对象才可以被抛出。
三、异常和捕获
当程序发生异常时,会立即终止,无法继续向下进行。为了保证程序能够有效的执行,Java中提供了一种对异常进行处理的方式——异常捕获。
java中的异常捕获一般是使用try…catch语句来完成的
举例
public class Demo{
public static void main(String[] args) {
int result = divide(3,0);
if (result == -1){
System.out.println("程序发生异常");
}else {
System.out.println(result);
}
}
public static int divide(int x,int y){
try {
int result = x/y;
return result;
}catch (Exception e){
System.out.println("捕获的异常信息为:"+e.getMessage());
}finally{
System.out.println("一定会执行的finally代码块");
}
return -1;
}
}
在程序中,有些时候会希望有些语句无论程序是否发生异常都要执行,这时就可以在try…catch语句后,加一个finally{}代码块。
应用
//老师用电脑讲课。
// 两个对象:
// 老师:属性:姓名。行为:讲课。
// 电脑:行为:运行。
// 考虑问题:电脑蓝屏-->异常。 电脑冒烟-->异常。
class Teacher{
private String name;
private Computer computer;
public Teacher(String name) {
this.name = name;
computer = new Computer();
}
public void speak() throws Normal {
try {
computer.run();
}catch (Blue b){
System.out.println(b);
throw new Normal(name + "老师因电脑蓝屏无法上课");
}catch (Yan y){
System.out.println(y);
throw new Normal(name + "老师因电脑冒烟无法上课");
}
}
}
class Computer {
private int state = 1;
public void run() throws Yan, Blue {
System.out.println("老师的电脑开始运行");
if (state == 1)
throw new Blue("电脑蓝屏了");
if (state == 2)
throw new Yan("电脑冒烟了");
}
public void reset() {
state = 0;
System.out.println("电脑重新启动");
}
}
class Blue extends Exception{
public Blue(){
super();
}
public Blue(String messages){
super(messages);
}
}
class Yan extends Exception{
public Yan(){
super();
}
public Yan(String messages){
super(messages);
}
}
class Normal extends Exception{
public Normal(){
super();
}
public Normal(String messages){
super(messages);
}
}
public class Demo01 {
public static void main(String[] args) {
Teacher t = new Teacher("马");
try {
t.speak();
}catch (Normal n){
System.out.println(n);
System.out.println("老师解决不了");
System.out.println("下课");
}
}
}
四、throws关键字和throw关键字
定义除法运算的时候,开发者通常会意识到可能出现的异常,可以直接通过try…catch对异常进行捕获处理,但有些时候,方法中的代码是否会出现异常,开发者并不明确或者不急于处理,为此,java允许将这种异常从当前方法中抛出,然后让后续的调用者在使用时在进行异常处理。
区别
:throw用在函数内,throws用在函数上.
throw抛出的是异常对象。throws用于进行异常类的声明,后面异常类可以有多个,用逗号分割。
throws举例
public class Demo{
//两个整数相除的方法,并用throws关键字抛出异常
public static int divide(int x,int y)throws Exception{
int result = x/y;
return result;//返回结果
}
//调用方法,并使用throws关键字继续抛出异常
public static void main(String[] args)throws Exception{
int result = divide(3,0);
System.out.println(result);
}
}
throw举例
public class Demo {
public static void printAge(int age) throws Exception {
if (age <= 0){
//当年龄为负数时,抛出异常
throw new Exception("输入年龄有误,输入年龄必须是正整数");
}else {
System.out.println("此人年龄为"+age);
}
}
public static void main(String[] args) {
int age = -1;
//try...catch语句用于捕获异常
try {
printAge(age);
//对捕获的异常处理
}catch (Exception e){
System.out.println("捕获的异常信息为"+e.getMessage());
}
}
}