发表于: 2019-11-04 22:22:44
1 939
今天完成的事情:JAVA基础学习:异常
public class Demo1_Exception {
public static void main(String[] args) {
//demo1();
}
//运行时报出NullPointException(空指针异常)和ArrayIndexOutOfBoundsException
public static void demo1() {
int[] arr = {11,22,33,44,55};
//arr = null; //NullPointerException 空指针异常
System.out.println(arr[10]); //ArrayIndexOutOfBoundsException 数组索引越界异常
}
}
ic static void main(String[] args) {
Demo2 d = new Demo2();
try{
int x = d.div(10, 0);
System.out.println(x);
}catch(ArithmeticException a) { //ArithmeticException a = new ArithmeticException();
System.out.println("出错了,除数为零了");
}
System.out.println("1111111111111111");
}
}
class Demo2 {
/*
* 除法运算
*/
public int div(int a,int b) { //a = 10,b = 0
return a / b;
* final,finally和finalize的区别
* final可以修饰类,不能被继承
* 修饰方法,不能被重写
* 修饰变量,只能赋值一次
*
* finally是try语句中的一个语句体,不能单独使用,用来释放资源
*
* finalize是一个方法,当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。
* B:面试题2
* 如果catch里面有return语句,请问finally的代码还会执行吗?如果会,请问是在return前还是return后。
*/
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.method());
}
}
class Demo {
public int method() {
int x = 10;
try {
x = 20;
System.out.println(1/0);
return x;
} catch (Exception e) {
x = 30;
return x;
} finally {
x = 40;
//return x; //千万不要在finally里面写返回语句,因为finally的作用是为了释放资源,是肯定会执行的
//如果在这里面写返回语句,那么try和catch的结果都会被改变,所以这么写就是犯罪
}
看<深入浅出Mybatis>
明天计划的事情:学习Spring 继续学习<深入浅出Mybatis>
遇到的问题:前几天写的SQL,多表关联查询有点忘记了...又回过头练习了一下
仿写的mybatis案例 代码有些地方不理解略过了 回头仔细检查一下
收获:学习了异常 怎么处理异常 复习了SQL语句 和 Mybatis
评论