【203天】黑马程序员27天视频学习笔记【Day19-中】
文章目录
叨逼叨两句
现在真是个好时代啊。
看的懂代码、理解人性绝对是巨大的优势
19-06:Throwable的几个常见方法
不常用
getMessage()
toString()
printStackTrace()
package com.test.demo001;
public class Demo31 {
public static void main(String[] args) {
int a = 10;
int b = 0;
try{
System.out.println(a/b);
} catch(Exception e){ //这里相当于Exception e = new ArithmeticException("/by zero");
System.out.println(e.getMessage());//打印异常信息,返回值String
System.out.println(e.toString());//打印异常信息与类名,返回值String
e.printStackTrace();//打印异常信息、类名与异常位置,JVM默认使用的就是这个,返回值void
}
}
}
19-07:throws方式进行异常处理
RuntimeException抛出到主方法可以不用写throws
package com.test.demo001;
public class Demo31 {
public static void main(String[] args) {
Student s = new Student();
s.setAge(-1);
System.out.println(s.getAge());
}
}
public void setAge(int age) {
if(age > 0 && age < 150){
this.age = age;
} else {
throw new RuntimeException("年龄非法");
}
}
其它异常需要抛出来
package com.test.demo001;
public class Demo31 {
public static void main(String[] args) throws Exception {
Student s = new Student();
s.setAge(-1);
System.out.println(s.getAge());
}
}
public void setAge(int age) throws Exception {
if(age > 0 && age < 150){
this.age = age;
} else {
throw new Exception("年龄非法");
}
}
19-08:throw的概述以及和throws的区别
throw
后面可跟多个异常,用逗号隔开
后面跟的是类名
用在方法外
throws
后面只能跟一个异常
后面跟的是对象
用在方法内
public void setAge(int age) throws Exception,RuntimeException {
if(age > 0 && age < 150){
this.age = age;
} else {
throw new Exception("年龄非法");
//上面这条语句相当于
//Exception e = new Exception("年龄非法");
//throw e
}
}
19-09:finally关键字的特点及作用
finally的特点
被finally控制的语句一定会执行
-
特殊情况:在执行到finally之前jvm退出(比如System.exit(0))
package com.test.demo001; public class Demo31 { public static void main(String[] args) throws Exception { int a = 10; int b = 0; try{ System.out.println(a/b); } catch(Exception e){ System.out.println("catch执行"); System.exit(0); return; } finally { System.out.println("finally执行"); } } }
finally的作用
用于释放资源,在IO流操作和数据库操作中会见到
finally遇到return
return相当于方法的最后一口气,方法挂之前,会看看有没有finally帮其完成遗愿,有的话就完成finally后再彻底返回。
package com.test.demo001;
public class Demo31 {
public static void main(String[] args) throws Exception {
int a = 10;
int b = 0;
try{
System.out.println(a/b);
} catch(Exception e){
System.out.println("catch执行");
return;
} finally {
System.out.println("finally执行");
}
}
}
19-10:finally关键字面试题
-
final,finally和finalize的区别
-
final
修饰类,不能被继承
修饰方法,不能被重写
修饰变量,只能赋值一次
finally是try语句中的一个语句体,不能单独使用,用来释放资源
finallize是一个方法,当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。
-
finally里不要写return,那样会让try和catch里的return都不再执行了。【一个方法执行完一个return就结束了】
package com.test.demo001;
public class Demo31 {
public static void main(String[] args) throws Exception {
Demo31 d = new Demo31();
int x = d.method();
System.out.println(x);
}
public int method(){
int a = 10;
try{
a = 20;
System.out.println(1/0);
return a;
} catch(Exception e){
a = 30;
return a;
} finally {
a = 40;
return a;
}
}
}
以下的代码结果是30,finally里的语句虽然可以运行,但他已经影响不了在catch中等待返回的a了【不记得了就看视频19-10的讲解】
package com.test.demo001;
public class Demo31 {
public static void main(String[] args) throws Exception {
Demo31 d = new Demo31();
int x = d.method();
System.out.println(x);
}
public int method(){
int a = 10;
try{
a = 20;
System.out.println(1/0);
return a;
} catch(Exception e){
a = 30;
return a;
} finally {
a = 40;
}
}
}
19-11:自定义异常
三步曲
要定义一个名字像异常的类
-
要继承某个异常类
继承Exception:编译器需要处理异常.
继承RuntimeException:运行时处理异常.
编写构造函数
package com.test.demo001;
public class Demo31 {
public static void main(String[] args){
Student s = new Student();
s.setAge(-10);
}
}
class AgeOutOfException extends RuntimeException{
public AgeOutOfException() {
super();
}
public AgeOutOfException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public AgeOutOfException(String message, Throwable cause) {
super(message, cause);
}
public AgeOutOfException(String message) {
super(message);
}
public AgeOutOfException(Throwable cause) {
super(cause);
}
}
public void setAge(int age) throws AgeOutOfException {
if(age > 0 && age < 150){
this.age = age;
} else {
throw new AgeOutOfException("年龄非法");
//上面这条语句相当于
//Exception e = new Exception("年龄非法");
//throw e
}
}
19-12:异常的注意事项及如何使用异常处理
-
异常注意事项
子类重写父类方法,子类方法中抛出的异常必须与父类相同,或者是父类异常的子类(异常界,父王最坏)
如果父类抛出多个异常,子类重写父类时,只能抛出相同的异常或者是他的子类,子类不能抛出父类没有的异常。
如果被重写的方法没有异常抛出,那么子类的方法绝对不可以抛出异常,如果此时子类中有异常发生,那么子类只能try,不能throws。
-
如何使用异常处理
原则:如果该功能内部能解决,用try;处理不了,用throws。
区别:后续代码还要继续执行,用try;后续代码不执行了,用throws【将终止程序运行】。
如果JDK没有提供对应的异常,需要自定义异常
19-13:练习
package com.test.demo001;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Demo009 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("Please");
String line = sc.nextLine();
try{
int num = Integer.parseInt(line);
System.out.println(num);
break;
} catch(Exception e){
try {
new BigInteger(line);
System.out.println("您输入的整数太大了");
} catch (Exception e1) {
try{
new BigDecimal(line);
System.out.println("您不能输入小数");
} catch(Exception e2){
System.out.println("非法字符");
}
}
}
}
}
}
异常个人总结:组成异常的三部分
一个异常类【关键是类名+构造函数】
一个抛出异常的方法【明确何种情况抛异常】
一个调用2中方法的主函数【明确对该方法到底是try处理还是throws给别人处理】
package com.test.demo001;
import java.util.Scanner;
public class Demo31 {
public static void main(String[] args) throws AgeOutOfException{
demo02();
}
private static void demo01() throws AgeOutOfException {
throw new AgeOutOfException("YC");
}
private static void demo02() throws AgeOutOfException {
demo01();
}
}
class AgeOutOfException extends Exception{
public AgeOutOfException() {
super();
}
public AgeOutOfException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public AgeOutOfException(String message, Throwable cause) {
super(message, cause);
}
public AgeOutOfException(String message) {
super(message);
}
public AgeOutOfException(Throwable cause) {
super(cause);
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论