1.异常
1.1生活中的异常
人的一生 会经历伤病
医生会问?咋啦?
你头疼
脑部ct
医生诊断
完以后。治好这个病。
1.2Java中的异常
程序在自上而下执行的时候,发生了不可预期的事件,这个事件阻止程序的运行。这就是异常。
数组下标越界 类转换异常
Java给咱们封装很多的异常类,并且提供很优秀的异常处理机制。
Java提供了一个类Throwable
1.3Throwable类
构造方法:
Throwable()
构造一个新的可抛出的null
作为其详细信息。
Throwable(String message)
构造一个具有指定的详细消息的新的throwable。
方法:
String
getMessage()
返回此throwable的详细消息字符串。void
printStackTrace()
将此throwable和其追溯打印到标准错误流。String
toString()
返回此可抛出的简短描述。
package com.qfedu.b_trhowable;
public class Demo1 {
public static void main(String[] args) {
System.out.println("嘻嘻");
System.err.println("哈哈");
Throwable throwable = new Throwable();
System.out.println(throwable.getMessage());
Throwable throwable2 = new Throwable("哈哈,我傻逼了");
System.out.println(throwable2.getMessage());
/**
* java.lang.Throwable: 哈哈,我傻逼了
at com.qfedu.b_trhowable.Demo1.main(Demo1.java:7)
*/
throwable2.printStackTrace();
//java.lang.Throwable: 哈哈,我傻逼了
//告知了这个错误信息
System.out.println(throwable2.toString());
}
}
1.4错误和异常
Throwable 下面有两个子类 一个叫Error 一个叫Exception
Error:是代表JVM本身的错误,咱们程序员是通过代码解决不了的。
Exception: 异常,代表程序在运行过程中,发生了不可预期的事件。可以使用Java来出来,让他继续执行下去。
异常分为两种:
编译时异常:
FileNotFoundException
SQLException
ClassNotFoundException
InterruptException
运行时异常:
数组下标越界 ArrayIndexOutOfBoundException
类转换异常 ClassCastException
空指针异常 NullPointerException
1.5异常【重点】
代码有可能会出现异常。Java给咱们提供了两种解决方案
1.异常的捕捉
2.异常的抛出
1.5.1异常的捕捉
在程序运行过程中,代码难免有可能会遇到异常。如果没有异常,代码正常执行。
如果有异常,就捕捉异常
语法格式:
try { 有可能出现异常的代码 } catch (异常对象) { //针对于面异常的处理方案 }
package com.qfedu.c_Exception;
public class Demo2 {
public static void main(String[] args) {
test(3, 0);
}
public static void test (int a, int b) {
int ret = 0;
try {
//有可能个出现异常的代码
ret = a / b;
} catch (ArithmeticException e) {
System.out.println("123");
//打印错误信息
System.out.println(e.getMessage());
}
//即使代码有异常,通过捕捉以后。是不会影响程序的接着执行的代码的
System.out.println(ret);
System.out.println( 4 + 4);
}
}
package com.qfedu.c_Exception;
public class Demo3 {
public static void main(String[] args) {
int[] arr = new int[2];//数组的容量是2
test(1, 1, arr);
}
public static void test (int a, int b, int[] arr) {
int ret = 0;
try {
ret = a / b;//有可能出现的异常的代码
arr[2] = 89;//这个会有异常
//jvm造出来哪个异常对象,就去catch 到哪个异常然后去执行具体的catch
} catch (ArithmeticException e) {
System.out.println("123");
System.out.println(e.getMessage());
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("456");
System.out.println(e.getMessage());
}
System.out.println(4 + 8);
}
}
继续改进
package com.qfedu.c_Exception;
public class Demo4 {
public static void main(String[] args) {
int[] arr = new int[2];//数组的容量是2
test(1, 1, arr);
}
public static void test (int a, int b, int[] arr) {
int ret = 0;
try {
ret = a / b;//有可能出现的异常的代码
arr[2] = 89;//这个会有异常
//jvm造出来哪个异常对象,就去catch 到哪个异常然后去执行具体的catch
} catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
System.out.println("123");
System.out.println(e.getMessage());
}
System.out.println(4 + 8);
}
}
改进的最终版本
package com.qfedu.c_Exception;
public class Demo5 {
public static void main(String[] args) {
int[] arr = new int[2];//数组的容量是2
test(1, 0, arr);
}
public static void test (int a, int b, int[] arr) {
int ret = 0;
try {
ret = a / b;//有可能出现的异常的代码
arr[2] = 89;//这个会有异常
//jvm造出来哪个异常对象,就去catch 到哪个异常然后去执行具体的catch
} catch (Exception e) {
System.out.println("123");
System.out.println(e.getMessage());
}
System.out.println(4 + 8);
}
}
try-catch-finally
package com.qfedu.c_Exception;
public class Demo5 {
public static void main(String[] args) {
int[] arr = new int[2];//数组的容量是2
test(1, 0, arr);
}
public static void test (int a, int b, int[] arr) {
int ret = 0;
try {
ret = a / b;//有可能出现的异常的代码
arr[2] = 89;//这个会有异常
//jvm造出来哪个异常对象,就去catch 到哪个异常然后去执行具体的catch
} catch (Exception e) {
System.out.println("123");
System.out.println(e.getMessage());
} finally {
//无论有没有异常,最终都要执行的
System.out.println(4 + 8);
}
}
}
1.5.2异常的抛出
在代码出现异常的地方进行异常的抛出
如果异常的抛出的话,一旦发生异常,从出现异常的地方会终止代码
使用两个关键字:
throw: 在方法中抛出一个异常。自己造一个错
throws: 在方法的声明处书写,告知当前调用者,此处有异常。要小心
package com.qfedu.c_Exception;
import java.io.FileNotFoundException;
public class Demo8 {
public static void main(String[] args) throws Exception{
test(0);
Thread.sleep(1000);
}
public static void test (int a) throws FileNotFoundException{
if (a == 0) {
//编译时异常
throw new FileNotFoundException();
}
System.out.println("jvm xixi");
}
}
总结:
关于异常:一阶段和二阶段 只需要会咋抛的或者咋捕捉的即可
1.6自定义异常
Java给咱们提供了很多的异常对象,但是有的时候还是满足不了现实生活的需求,我自己造异常对象。
继承Exception
需求:
如果是单身 抛出一个异常 单身异常类
package com.qfedu.c_Exception;
import java.util.Scanner;
public class Demo11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个整数:");
int score = scanner.nextInt();
try {
test(score);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void test (int score) throws Exception {
if (score > 100 || score < 0) {
throw new Exception("输入的整数有误的。。。");
}
if (score >= 90 && score <= 100) {
System.out.println("优秀");
} else if (score >= 80) {
System.out.println("良好");
} else if (score >= 70) {
System.out.println("及格");
} else {
System.out.println("叫家长。。。");
}
}
}
2.String类
2.1String类型的两种的声明方式
package com.qfedu.d_string;
public class Demo1 {
public static void main(String[] args) {
//声明字符串的两种方式
String str = "狗蛋";
String str2 = "狗蛋";
System.out.println(str);
String str1 = new String("狗蛋");
System.out.println(str1);
System.out.println(str == str2);//true
//== 比较的是内存地址
System.out.println(str == str1);//false
//equal比较的是地址,如果地址不一样 再去比较内容。如果内容一样就是true
//开发中字符串的比较使用的是equals
System.out.println(str.equals(str1));//true
}
}