一、泛型
1. 泛型类
class MyArrayList<E> { //泛型类
public E e;
MyArrayList() {
//e = "abc"; //报错,因为构造方法尚未确定类型
}
public E method(E e) {
this.e = e;
System.out.println(e);
return this.e;
}
}
public class Test {
public static void main(String[] name) {
MyArrayList<String> arr = new MyArrayList<>();
String s = arr.method("abc");
}
}
2. 泛型方法
class MyArrayList{
public <E> E method(E e) { //泛型方法
System.out.println(e);
return e;
}
}
public class Test {
public static void main(String[] name) {
MyArrayList arr = new MyArrayList();
String s = arr.method("abc");
}
}
3. 泛型接口
interface IA<E> { //泛型接口
public static final String e = "abc";
public abstract E method1(E e);
public default E method2(E e) {
System.out.println(e);
return e;
}
}
class IMP1 implements IA<String> { //实现类时确定泛型
@Override
public String method1(String s) {
System.out.println(s);
return s;
}
@Override
public String method2(String s) {
return e;
}
}
class IMP2<E> implements IA<E> { //创建对象时确定泛型
@Override
public E method1(E e) {
System.out.println(e);
return e;
}
@Override
public E method2(E e) {
return e;
}
}
public class Test {
public static void main(String[] name) {
IMP1 imp1 = new IMP1(); //泛型已确定
String s1 = imp1.method1("abc");
s1 = imp1.method2("abc");
IMP2<String> imp2 = new IMP2<>(); //泛型未确定
String s2 = imp2.method1("abc");
s2 = imp2.method2("abc");
}
}
4. 泛型通配符
ArrayList<Object> arr1 = new ArrayList<>();
ArrayList<Number> arr2 = new ArrayList<>();
ArrayList<Integer> arr3 = new ArrayList<>();
ArrayList<String> arr4 = new ArrayList<>();
//ArrayList arr
method1(arr1);
method1(arr2);
method1(arr3);
method1(arr4);
//ArrayList<?> arr ?通配符
method2(arr1);
method2(arr2);
method2(arr3);
method2(arr4);
//ArrayList<? extends Number> arr 高级通配符 ?+继承自Number类
method3(arr1); //报错
method3(arr2);
method3(arr3);
method3(arr4); //报错
//ArrayList<? super Integer> arr 高级通配符 ?+Integer父类
method4(arr1);
method4(arr2);
method4(arr3);
method4(arr4); //报错
}
public static void method1(ArrayList arr) {
return;
}
public static void method2(ArrayList<?> arr) {
return;
}
public static void method3(ArrayList<? extends Number> arr) {
return;
}
public static void method4(ArrayList<? super Integer> arr) {
return;
}
二、异常
1. 概述
异常 :指的是程序在执行过程中,出现的非正常的情况,如果不处理最终会导致JVM的非正常停止。
**处理方式:**对于异常,一般有两种解决方法:一是遇到错误就终止程序的运行。另一种方法是由程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理。
Java中的异常实际上就是用不同的类表示,一旦发生某种异常,就通过创建该异常类型的对象,并且抛出,然后程序员可以catch到这个异常对象,并处理,如果无法catch到这个异常对象,那么这个异常对象将会导致程序终止。
1.1 体系
异常的根类是java.lang.Throwable
,其下有两个子类:java.lang.Error
与java.lang.Exception
,平常所说的异常指java.lang.Exception
。
- Error:严重错误Error,无法通过处理的错误,只能事先避免。
- 例如:
StackOverflowError
和OOM
(OutOfMemoryError)。
- 例如:
- Exception:表示异常,其它因编程错误或偶然的外在因素导致的一般性问题,程序员可以通过代码的方式纠正,使程序继续运行,是必须要处理的。
- 例如:空指针访问、试图读取不存在的文件、网络连接中断、数组角标越界
常用方法
public void printStackTrace(); //打印异常的详细信息。
public String getMessage(); //获取发生异常的原因。
1.2 分类
异常(Exception)的分类:根据在编译时期还是运行时期去检查异常
- 编译时期异常:checked异常。在编译时期,就会检查,如果没有处理异常,则编译失败。(如日期格式化异常)
- 运行时期异常:runtime异常。在运行时期,检查异常.在编译时期,运行异常不会被编译器检测到(不报错)。(如数组索引越界异常,类型转换异常)。
编译时期异常与运行时期异常的区别与意义:
- 之所以要设定编译时期异常,是因为程序在编写时考虑到,即便是传入的参数没问题,也有可能会导致程序出错(如传入的网址时google.com,网址参数没问题但实际上无法访问),为了避免这个问题,就必须在一开始就完成对异常的处理
- 而运行时期异常说明若参数传入的没问题,那一定不会出现问题,所以没必要提前避免异常,而若参数传入有问题那就是语法错误了。
2. 异常抛出与处理
2.2 抛出throw
Java提供了一个throw关键字,用来抛出一个指定的异常对象。throw
用在方法内来抛出一个异常对象,将这个异常对象传递到调用者并结束当前方法的执行。
throw new NullPointerException("空指针异常");
throw new ArrayIndexOutOfBoundsException("数组访问越界");
public class Test {
public static void main(String[] args) {
int[] arr = {10,20,30,40};
method(arr,4);
}
public static void method(int[] arr,int index){
if (index < 0 || index > arr.length-1){ // 索引不存在-->抛出一个异常
throw new ArrayIndexOutOfBoundsException(index+"");
}else{
int num = arr[index];
System.out.println(num);
}
}
}
2.2 处理
异常声明throws
使用throws
关键字将问题标识出来, 表示当前方法不处理异常,而是提醒让调用者来处理,若都不处理最终会到虚拟机,虚拟机直接结束程序,打印异常信息。
异常捕获try…catch
对异常进行捕获处理 , 处理完后程序可以正常向下执行。执行步骤:
- 首先执行
try
中的代码,如果try
中的代码出现了异常,则出现异常位置后面的代码就不会再执行了,而是会直接执行catch()
里面的代码,执行完后程序继续执行try
后面的代码。 - 如果
try
中的代码没有出现异常,那么就不会执行catch()
里面的代码,而是继续往下执行
try{
编写可能会出现异常的代码
}catch(异常类型 e){
处理异常的代码
//记录日志/打印异常信息
}
finally代码块
因为异常会引发程序跳转,导致有些语句执行不到,而finally
代码块中存放的代码除非try
或者catch
中调用退出JVM的相关方法,否则无论异常是否发生都是一定会被执行的。
- 首先执行
try
中的代码,如果try
中的代码出现了异常,那么就直接执行catch()
里面的代码,执行完后会执行finally中的代码,然后程序继续往下执行 - 如果
try
中的代码没有出现异常,那么就不会执行catch()
里面的代码,但是还是会执行finally
中的代码,然后程序继续往下执行
try{
可能会出现异常的代码
}catch(异常的类型 变量名){
处理异常的代码或者打印异常的信息
}finally{
无论异常是否发生,都会执行这里的代码
(正常情况,都会执行finally中的代码,一般用来释放资源)
}
public class Test {
public static void main(String[] args) {
method1();
/**************捕获异常**************/
try { // 捕获处理运行异常
System.out.println(1/1);// 无异常
}catch (ArithmeticException e){
System.out.println("数学运算异常");
}finally { //finally代码块
System.out.println("finally 无论是否发生异常都会执行");
}
try {
System.out.println(1/0);// 有异常
System.out.println("try...");
}catch (ArithmeticException e){
System.out.println("数学运算异常");
}finally { //finally代码块
System.out.println("finally 无论是否发生异常都会执行");
}
/**************声明异常**************/
// 一次声明处理一个异常
public static void method1() throws ParseException{
throw new ParseException("解析异常",1);
}
// 一次声明处理多个异常
public static void method2(int num) throws ParseException,FileNotFoundException{
if (num == 1){
throw new ParseException("解析异常",1);
}else{
throw new FileNotFoundException("文件找不到异常");
}
}
}
3. 自定义异常
3.1 自定义异常
若遇到如例如年龄负数问题,考试成绩负数等这类问题时,系统没有定义相关的异常,因此需要自定义异常类。自定义异常类只要继承系统的Exception
类即可
-
自定义编译期异常:继承
java.lang.Exception
public class MyException1 extends Exception { public MyException1() { } public MyException1(String message) { super(message); } }
-
自定义运行期异常:继承
java.lang.RuntimeException
。public class MyException2 extends RuntimeException { public MyException2() { } public MyException2(String message) { super(message); } }
*/
public class Test {
public static void main(String[] args) {
throw new MyException1("自定义异常1");
throw new MyException2("自定义异常2");
}
}
3.2 异常注意事项
- 若父类的方法抛出了多个异常,子类覆盖(重写)父类方法时,只能抛出相同的异常或者其子集。
- 若父类方法没有抛出异常,子类覆盖父类该方法时也不可抛出异常。此时子类若产生该异常,只能捕获处理,不能声明抛出。
- 声明处理多个异常时,可以直接声明这多个异常的父类异常
- 多个异常时,一般使用一次捕获多次处理,若
catch
中的多个异常之间有子父类异常的关系,那么子类异常要求在上面的catch
处理,父类异常在下面的catch
处理。
package com.itheima.demo12_异常注意事项;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
/**
* @Author:pengzhilin
* @Date: 2020/9/16 15:49
*/
class Fu {
public void show() throws ParseException, FileNotFoundException {
}
public void run() {
}
}
class Zi extends Fu {
@Override
public void run() {
try {
throw new FileNotFoundException("");
}catch (FileNotFoundException e){
}
}
}
public class Test {
public static void main(String[] args) {
}
// 1.运行时异常被抛出可以不处理。即不捕获也不声明抛出。
public static void show1() {
System.out.println(1 / 0);
}
// 4.声明处理多个异常,可以直接声明这多个异常的父类异常
public static void show2(int num) throws Exception{
if (num == 1){
throw new FileNotFoundException("");
}else{
throw new ParseException("",1);
}
}
// 多个异常使用捕获
// 1. 多个异常分别处理。
public static void show3(int num) {
if (num == 1){
try {
throw new FileNotFoundException("");
} catch (FileNotFoundException e) {
}
}else{
try {
throw new ParseException("",1);
} catch (ParseException e) {
}
}
}
// 2. 多个异常一次捕获,多次处理。
public static void show4(int num) {
try {
if (num == 1){
throw new FileNotFoundException("");
}else{
throw new ParseException("",1);
}
}catch (FileNotFoundException e){
}catch (ParseException e){
}
}
// 3. 多个异常一次捕获一次处理。
public static void show5(int num) {
try {
if (num == 1){
throw new FileNotFoundException("");
}else{
throw new ParseException("",1);
}
}catch (Exception e){
}
}
// 7.当多异常分别处理时,捕获处理,前边的类不能是后边类的父类
public static void show6(int num) {
try {
if (num == 1){
throw new FileNotFoundException("");
}else{
throw new ParseException("",1);
}
}catch (Exception e){
}catch (ParseException e){
}
}
}
throw new ParseException("",1);
}
}catch (Exception e){
}
}
// 7.当多异常分别处理时,捕获处理,前边的类不能是后边类的父类
public static void show6(int num) {
try {
if (num == 1){
throw new FileNotFoundException("");
}else{
throw new ParseException("",1);
}
}catch (Exception e){
}catch (ParseException e){
}
}
}