// 1. 类型转换出现了错误
String s = "123";
//Integer i = (Integer)s;
Integer i = Integer.parseInt(s);
// 2.递归没有跳出的逻辑: StackOverFlowError:Error 错误
// 尽量要避免
//test();
// 3.访问一个为空对象的成员方法时,出现了错误:java.lang.NullPointerException(异常)
// Java中异常分为2大类:
// 3.1 可以通过代码恢复正常逻辑执行的异常,称之为运行期异常:RuntimeException
// 3.2 不可以通过代码恢复正常逻辑执行的异常,称之为编译期异常:Exception
User user = null;
System.out.println(user.toString());
}
public static void test() {
test();
}
}
class User {
}
/*
异常处理语法:
TODO try : 尝试
TODO catch : 捕捉
捕捉多个异常的时候,需要先捕捉范围小的异常,然后再捕捉范围大的异常
TODO finally : 最终
try {
可能会出现异常的代码
如果出现异常,那么JVM会将异常进行封装,形成一个具体的异常类,然后将这个异常抛出
所有的异常对象都可以被抛出
} catch ( 抛出的异常对象 对象引用 ) {
异常的解决方案
} catch () {
} finally {
最终执行的代码逻辑
}
*/
int i = 0;
int j = 0;
try {
j = 10 / i;
} catch ( ArithmeticException e) {
//e.getMessage(); // 错误的消息
//e.getCause(); // 错误的原因
e.printStackTrace();
i = 10;
j = 10 / i;
} finally {
System.out.println("最终执行的代码");
}
System.out.println(j);
// 1.除数为0的算术异常:java.lang.ArithmeticException
// 运行期异常
// int i = 0;
// if ( i != 0 ) {
// int j = 10 / i ;
// }
// 2.空指针异常 :java.lang.NullPointerException
// 调用了一个为空(null)对象的成员属性或成员方法时,就会发生异常
User3 user = null;
// if ( user != null ) {
// System.out.println(user.toString());
// }
try {
//System.out.println(user.toString());
System.out.println(user.name);
} catch (NullPointerException e) {
System.out.println("对象为空,需要分析数据为空的原因");
}
}
}
class User3 {
public static String name = "zhangsan";
}
// 3.索引越界 : ArrayIndexOutOfBoundsException
String [] names = new String[3];
names[0] = "zhangsan";
names[1] = "lisi";
names[2] = "wangwu";
if ( names.length == 4) {
names[3] = "zhaoliu";
}
for ( int i = 0; i < names.length; i++ ) {
System.out.println(names[i]);
}
// 4.字符串索引越界:StringIndexOutOfBoundsException
String s = "abc";
//System.out.println(s.charAt(3));
System.out.println(s.substring(3));
// 5.格式化异常:NumberFormatException
// String s = "a123";
// Integer i = Integer.parseInt(s);
//
// System.out.println(i);
// 6.类型转换错误: ClassCastException
Object obj = new User5();
if ( obj instanceof Emp5 ) {
Emp5 emp = (Emp5)obj;
}
}
}
class User5 {
}
class Emp5 {
}
User6 user = new User6();
int i = 10;
int j = 0;
try {
user.test(i,j);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class User6 {
// 如果方法中可能会出现问题,那么需要提前声明,告诉其他人,我的方法可能会出问题
// 此时需要使用关键字throws
// 如果程序中需要手动抛出异常对象,那么需要使用throws关键字,然后new出异常对象
public void test( int i, int j ) throws Exception {
try {
System.out.println( i / j );
} catch (ArithmeticException e) {
throw new Exception();
}
String account = "zhangsan";
String password = "123123";
try {
login(account,password);
} catch (AccountException accountException) {
System.out.println("账号不正确,需要修正");
} catch (PasswordException passwordException) {
System.out.println("密码不正确,需要修正");
} catch (LoginException loginException) {
System.out.println("其他登录的相关错误,需要确认");
}
}
public static void login(String account,String password) {
if ( !"admin".equals(account) ) {
throw new AccountException("账号不正确");
}
if ( !"admin".equals(password) ) {
throw new PasswordException("密码不正确");
}
System.out.println("登陆成功");
}
}
class AccountException extends LoginException {
public AccountException (String message) {
super(message);
}
}
class PasswordException extends LoginException {
public PasswordException (String message) {
super(message);
}
}
// TODO 自定义异常
class LoginException extends RuntimeException {
public LoginException (String message) {
super(message);
}
}