Throwable
异常:可以补救,可恢复的错误 Exception类
错误:不可恢复的问题 Error类
常见的异常
AWTException swing组件的异常
ClassNotFoundException 找不到类的异常
IOException IO流出现异常
NoSuchFieldException 没有匹配的属性
NoSuchMethodException 没有匹配的方法
SQLException 访问数据库的异常
RuntimeException 运行时异常
ClassCastException 类型转换异常,在强制转型的时候会出现
NullPointerException 空指针异常
Student stu;
stu.study();
IndexOutOfBoundsException:下标超出边界
数组,二维数组,ArrayList,字符串,向量
异常的分类:
强制检测异常(强制捕获)
非强制检测异常(运行时异常)
自定义异常方法
public void guessNum2(int num) throws Exception{
throw new Exception("异常信息");
}
如何强制检测异常:
处理方式一:
try{
//首选方案
}catch(Exception ef){
//备选方案
}
处理方式二:
在调用这个方法的方法上throws
运行时异常:
不需要强制检测,在需要的时候也可以检测
**********************************************************
如何查看程序错误:
程序的执行
顺序(按照代码顺序一行一行执行)
分支(if..else,switch...case...,try...catch...)
循环(for while do...while...)
自定义抛出异常:
public class Demo {
public static void main(String args[]) {
// guessNum(10000);
int num = 10;
// guessNum2(num);
guessNum3(num);
}
public static void guessNum(int num) {
if (num < 0 || num >= 100) {
System.out.println("不符合要求!!");
} else {
System.out.println("您猜的是:" + num);
}
}
// 抛出强制检测异常,必须在定义方法的时候throws
public static void guessNum2(int num) throws Exception {
if (num < 0 || num >= 100) {
// 创建一个异常对象
Exception ex = new Exception("不符合要求!!");
// 抛出异常对象
throw ex;
}
System.out.println("您猜的是:" + num);
}
// 抛出运行时异常,在定义方法的时候不需要throws
public static void guessNum3(int num) {
if (num < 0 || num >= 100) {
// 创建一个运行时异常对象
RuntimeException ex = new RuntimeException("不符合要求!!");
// 抛出异常对象
throw ex;
}
System.out.println("您猜的是:" + num);
}
}
运行结果:
您猜的是:10
学会使用try-catch-finally来处理异常:
/*使用try-catch
*/
public class TryCatchTest {
public static void main(String[] args) {
int number,value;
try{
number = 0;
value = 3/number;
//可能会抛出ArithmeticException异常,使用catch老捕获这个异常
}catch(ArithmeticException e){
e.printStackTrace();
System.out.println("错误:被0整除");
}
}
}
equals()方法;
==
比较的是两个变量是不是引用的同一个地址
equals:
所有类都包含一个equals()方法,是从Object中继承来的
equals的返回值是true还是false取决于如何重写
equals的重写的基本原则
自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。
对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。
一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。
对于任何非空引用值 x,x.equals(null) 都应返回 false。
public class Student {
private static String name;
public Student(String name) {
this.name = name;
}
// 重写equals的方法
public boolean equals(Student obj) {
return Student.name.equals(obj.name);
}
}
public class Democracy {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
String s4 = new String("abc");
Integer t1 = 100;
Integer t2 = new Integer(100);
System.out.println(s1 == s2);
System.out.println(s2 == s3);
System.out.println(s3 == s4);
System.out.println(t1.equals(t2));
System.out.println("----------------------------------");
System.out.println(s1.equals(s2));
System.out.println(s2.equals(s3));
System.out.println(s3.equals(s4));
int a = 100;
int b = 100;
System.out.println(a == b);
System.out.println("----------------------------------");
Student stu1 = new Student("abc");
Student stu2 = new Student("abc");
System.out.println(stu1.equals(stu2));
}
}
运行结果:
true
false
false
true
----------------------------------
true
true
true
true
----------------------------------
true
本文详细介绍了Java中的异常处理机制,包括强制检测异常与非强制检测异常的区别、常见异常类型及其处理方式,并通过示例代码展示了try-catch-finally的具体应用。此外,还深入探讨了equals方法的工作原理及重写时需遵循的原则。
5945

被折叠的 条评论
为什么被折叠?



