《Java基础入门 极速版》
ch06 异常
【例题1】 编译错误、运行错误、运行异常举例
package cn.du.ch06;
public class Java01_Exception {
public static void main(String[] args) {
String s = "123";
Integer i = (Integer)s;
Integer j = Integer.parseInt(s);
System.out.println("运行错误 Error【杜老师整理】");
test();
System.out.println("异常 Exception【杜老师整理】");
User user = null;
System.out.println(user.toString());
}
public static void test() {
test();
}
}
class User {
}
异常处理语法:
try {
} catch ( 抛出的异常对象 对象引用 ) {
} catch () {
} finally {
}
【例题2】 异常处理举例
package cn.du.ch06;
public class Java02_Exception {
public static void main(String[] args) {
System.out.println("异常处理【杜老师整理】");
int i = 0;
int j = 0;
try {
j = 10 / i;
} catch (ArithmeticException e) {
System.out.println(" 异常消息:"+e.getMessage());
System.out.println(" 异常原因:"+e.getCause());
e.printStackTrace();
i = 10;
j = 10 / i;
} finally {
System.out.println(" 最终(finally中)执行的代码");
}
System.out.println("finally之后的语句:j="+j);
}
}
【例题3】除数为0的算术异常、空指针异常举例
package cn.du.ch06;
public class Java03_Exception {
public static void main(String[] args) {
System.out.println("除数为0的算术异常、空指针异常【杜老师整理】");
int i = 0;
if ( i != 0 ) {
int j = 10 / i;
}
User3 user = null;
if ( user != null ) {
System.out.println(user.toString());
}
}
}
class User3 {
public static String name = "杜老师";
}
【例题4】数组索引越界-字符串索引越界异常
package cn.du.ch06;
public class Java04_Exception {
public static void main(String[] args) {
System.out.println("数组索引越界-字符串索引越界【杜老师整理】");
String[] names = new String[3];
names[0] = "张三";
names[1] = "李四";
names[2] = "王五";
names[3] = "赵六";
if ( names.length == 4 ) {
names[3] = "赵六";
}
for ( int i = 0; i < names.length; i++ ) {
System.out.println(names[i]);
}
String s = "abc";
System.out.println(s.charAt(3));
System.out.println(s.substring(4));
}
}
【例题5】数值格式化异常-类型转换错误
package cn.du.ch06;
public class Java05_Exception {
public static void main(String[] args) {
System.out.println("数值格式化异常-类型转换错误【杜老师整理】");
String s = "a123";
Integer i = Integer.parseInt(s);
System.out.println("i = "+i);
Object obj = new User5();
Emp5 emp = (Emp5)obj;
if ( obj instanceof Emp5 ) {
Emp5 emp2 = (Emp5)obj;
}
}
}
class User5 {}
class Emp5 {}
【例题6】常见异常
package cn.du.ch06;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.SocketException;
import java.sql.SQLException;
public class Java06_Exception {
public static void main(String[] args) {
}
}
class User6 {
public void test() {
try {
this.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
【例题7】抛出异常
package cn.du.ch06;
public class Java07_Exception {
public static void main(String[] args) throws Exception {
System.out.println("抛出异常throw-throws【杜老师整理】");
int i = 10;
int j = 0;
test(i, j);
}
public static void test( int i, int j ) throws Exception {
try {
System.out.println(i / j);
} catch (ArithmeticException e) {
throw new Exception("除数为0");
}
}
}
【例题8】自定义异常
package cn.du.ch06;
public class Java08_Exception {
public static void main(String[] args) throws Exception {
System.out.println("自定义异常【杜老师整理】");
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 ( !"dzjiang".equals(account) ) {
throw new AccountException("账号不正确");
}
if ( !"123456".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);
}
}
class LoginException extends RuntimeException {
public LoginException(String message) {
super(message);
}
}