Java中的异常

1.异常

1.1错误和异常

Java中的错误 Error: 代表JVM本身的错误, 程序员无法通过代码进行处理的Error很少出现,一旦出现 就意味着程序崩溃了。

Java中的异常 Exception: 代表Java程序在运行过程中出现了不可预期的错误,然后影响了代码的正常的执行,可以使用Java中的异常的处理机制来出来一下代码,让代码能够正常的执行下去。

1.2Throwable【开发不用】

Java是面向对象开发的,Java中封装好了一个类叫Throwable类,这个类是专门处理错误和异常的类。

构造方法:

Throwable()构造一个新的可抛出的 null作为其详细信息。

Throwable(String message)构造一个具有指定的详细消息的新的throwable。

方法:

String getMessage()返回此throwable的详细消息字符串。

void printStackTrace()将此throwable和其追溯打印到标准错误流。

public class Demo1 {
	public static void main(String[] args) {
		Throwable throwable = new Throwable();
		System.out.println(throwable);//java.lang.Throwable
		//构造一个新的可抛出的 null作为其详细信息的一个对象
		System.out.println(throwable.getMessage());//null
		
		//Throwable(String message)
		//构造一个具有指定的详细消息的新的throwable。
		Throwable throwable2 = new Throwable("狗蛋");
		System.out.println(throwable2.getMessage());//标准的输出流
		
		throwable2.printStackTrace();//没有返回值 是不能sout的
		//标准错误流是在控制台显示红色的 
		/**
		 * java.lang.Throwable: 狗蛋
			at com.qf.c_throwable.Demo1.main(Demo1.java:13)

		 */
		System.out.println("qwer");
		
	}
}

1.3异常【重点】

Java中封装好了处理异常的机制。

Java异常分为两大类:

编译时异常: 在写代码的编译器会报红,如:

FileNotFoundException

SQLException

ClassNotFoundExcetion

InterruptException

运行时异常: 在运行的时候 出现的异常,如:

ArrayIndexOutOfBoundsException 数组下标越界的异常

当Java代码出现了异常,然后就可以是使用Java的处理机制来处理,如果没有出现异常就正常执行即可。

1.3.1异常的捕捉

在程序运行过程中,难免会出现异常,这个时候Java中捕捉异常的语法格式进行异常的处理

语法格式:

try {//尝试
	可能出现异常代码
} catch(异常对象) {//抓
 //针对于上面异常的处理方案
}
执行流程: 如果try里面的代码没有异常,跳过catch 然后接着往下执行。
 如果trye里面有异常,就执行catch后面大括号的代码
public class Demo1 {
	public static void main(String[] args) {
		test(3, 0);
	}
	public static void test(int a, int b) {
//		Exception in thread "main" java.lang.ArithmeticException: / by zero
//		at com.qf.d_exception.Demo1.test(Demo1.java:8)
//		at com.qf.d_exception.Demo1.main(Demo1.java:5)
		int c = 0;
		try {
			c = a / b;
			
		}catch (Exception e) {//catch  抓 捕捉
			//Exception e = new ArithmeticException(); 多态
			//jvm会抛出一个异常的实例(对象)  将对象赋值给ArithmeticException  e  = new ArithmeticException()
			System.out.println("代码有异常,你自己看着办");
			System.out.println("除数不能为0");
		}
		System.out.println(c);
		
		System.out.println("嘻嘻");
	}
}

语法格式:

多个异常

try {
可能出现的异常的代码
} catch(异常对象1) {
	
} catch (异常对象2) {

}

try {
可能出现的异常的代码
} catch(异常对象1 | 异常对象2  e) {
	
}
public class Demo2 {
	public static void main(String[] args) {
		int[] arr = new int[2];
		test(3, 1, arr);
	}
	public static void test(int a, int b, int[] arr) {
		int c = 0;
		try {
			c = a / b;//除数为0  jvm抛出一个异常ArithmeticException
			arr[4] = 20;//jvm抛出一个异常ArrayIndexOutOfBoundsException
		} catch (ArithmeticException e) {
			System.out.println("除数不能为0");
			
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("数组下标越了界");
		}
		System.out.println("代码结束了");
		
		
	}
}
public class Demo3 {
	public static void main(String[] args) {
		int[] arr = new int[2];
		test(3, 0, arr);
	}
	public static void test (int a, int b, int[] arr) {
		int c = 0;
		try {
			c = a / b;//除数为0  jvm抛出一个异常ArithmeticException
			arr[4] = 20;//jvm抛出一个异常ArrayIndexOutOfBoundsException
		} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
			System.out.println("除数不能为0或者数组下标越界");
			
		}
		System.out.println("代码结束了");
	}
}

语法格式:

try {//尝试
	可能出现异常代码
} catch(异常对象) {//抓
 //针对于上面异常的处理方案
} finally {
	最终执行的代码
}

执行流程: 如果try里面的代码没有异常,跳过catch 然后接着往下执行。
 如果trye里面有异常,就执行catch后面大括号的代码
 finally代码无论有没有异常都要执行
public class Demo3 {
	public static void main(String[] args) {
		int[] arr = new int[2];
		test(3, 1, arr);
	}
	public static void test (int a, int b, int[] arr) {
		int c = 0;
		try {
			c = a / b;//除数为0  jvm抛出一个异常ArithmeticException
			arr[1] = 20;//jvm抛出一个异常ArrayIndexOutOfBoundsException
		} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
			System.out.println("除数不能为0或者数组下标越界");
			
		}finally {
			//最终的  无论有没有异常都要执行的
			System.out.println("代码结束了");
		}
		
	}
}

最终的版本

public class Demo4 {
	public static void main(String[] args) {
		int[] arr = new int[2];
		test(3, 0, arr);
	}
	public static void test (int a, int b, int[] arr) {
		int c = 0;
		try {
			c = a / b;//除数为0  jvm抛出一个异常ArithmeticException
			arr[4] = 20;//jvm抛出一个异常ArrayIndexOutOfBoundsException
		} catch (Exception e) {
			System.out.println("除数不能为0或者数组下标越界");
			System.out.println(e.getMessage());/// by zero
			
		}
		System.out.println("代码结束了");
		
		
	}
}

1.3.2异常的抛出

代码出现异常的地方进行抛出异常,特别是关于编译时异常的时候,为了保证咱们代码编译通过

可以使用异常抛出的语法格式来进行抛出异常

throws : 关键字 名词 告知调用者 此处有异常 一定注意一下 别写错了

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Demo5 {
	public static void main(String[] args)  throws Exception{
		
		//在当前出现异常代码所在的方法的后面 书写关键字 throws  异常类
		Thread.sleep(1000);
		
		FileInputStream fis = new FileInputStream(new File("c:/aaa/1.txt"));
		
		
	}
}

总结: 在有异常的地方的方法的后面 throws 异常类

1.3.3throw

抛的动作, 可以抛出来一个异常对象。 自己可以造错!!!

import java.util.Scanner;

public class Demo6 {
	public static void main(String[] args) throws Exception{
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String username = scanner.next();
		if (!username.equals( "狗蛋")) {
			//throw 是抛的动作  后面跟的是一个异常的对象
			//new Exception("用户名不存在");  编译时异常
			throw new Exception("用户名不存在");//造错!!!
			
		}
		System.out.println("请输入密码:");
		String password = scanner.next();
		if (!password.equals("123")) {
			throw new Exception("密码错误");
		}
		System.out.println("登陆成功!!!");
	}
	
}

1.3.4自定义异常

开发中会遇到很多的异常,但是Java中给咱们提供的异常不足以描述了。咱们可以自己造异常

//抄别人的异常类   单身人士的异常类
class SinglerException  extends Exception{
	public SinglerException () {
		super();
	}
	public SinglerException (String message) {
		super(message);
	}
	
	
}

public class Demo7 {
	public static void main(String[] args) throws Exception {
		buy(false);
	}
	//true  是单身   false 不是单身
	
	public static void buy (boolean isSingle) throws Exception{
		if (isSingle) {
			throw new SinglerException("单身不能进店购买");
		}
		System.out.println("情侣买一送一!!!");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值