我们有很多问题不是靠代码能够避免的,比如客户输入数据的格式,读取文件是否存在,网络是否保持通畅等
Java发现的异常可分为两类:


分为两类:
编译时异常(checked)
package baozhuang;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Date;
import java.util.Scanner;
import org.junit.Test;
public class ExceptionTest {
// NullPointerException 空指针
@Test
public void test1(){
int[] arr = null;
System.out.println(arr[3]);
}
// ArrayIndexOutOfBoundsException 数组角标越界
@Test
public void test2(){
int[] arr = new int[10];
System.out.println(arr[10]);
}
//ClassCastException 类型转换异常
@Test
public void test3(){
Object obj = new Date(0);
String str = (String)obj;
}
// NumberFormatException
@Test
public void test4(){
String str = "123";
str = "abc";
int num = Integer.parseInt(str);
}
// InputMismatchException 输入不匹配
@Test
public void test5(){
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt(); //输入abc
System.out.println(score);
}
// ArithmaticException 算数异常
@Test
public void test6(){
int a = 10;
int b = 0;
System.out.println(a/b);
}
// ***********************以下是编译异常****************************************
// 会在旁边打叉叉
@Test
public void test7(){
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file);
int data = fis.read();
while (data != -1){
System.out.println((char)data);
data = fis.read();
}
fis.close();
}
}

过程一: “抛” : 一旦出现异常,就会在异常代码处生成一个对应异常类的对象。
并将此对象抛出,一旦抛出对象以后,其后的代码就不再执行
过程二:“抓” : 可以理解为异常的处理方式 : try - catch - finally ,throws
try{
// 可能出现异常的代码
}catch(异常类型1 变量名1){
// 处理异常方式1
}catch(异常类型2 变量名2){
// 处理异常方式2
}catch(异常类型3 变量名3){
// 处理异常方式3
}
…
finally{
//一定会执行的代码
}
使用try将可能出现异常的代码包装起来,在执行过程中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,去catch中进行匹配
一旦try中的异常对象匹配到某一个catch时,就会进入catch中进行异常的处理,一旦处理完成,就会跳出try-catch结构(在没有写finally的情况下),继续执行其后的代码
异常处理catch要求有子父类关系,子类要在父类上面
常用异常处理对象如下 String getMessage 2. printStackTrace
在try结构中声明的变量,出了try结构以后,就不能再被调用
package baozhuang;
import org.junit.Test;
public class ExceptionTest1 {
@Test
public void test1(){
String str = "123";
str = "abc";
try{
int num = Integer.parseInt(str);
System.out.println("hello--------1"); // 无法执行
}catch(NumberFormatException e){
//System.out.println("出现数值转换异常了,不要着急....");
// 常用异常处理对象如下 String getMessage 2. printStackTrace
//System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("hello--------2"); // 可以执行
}
}
finally中声明的是一定会被执行的代码,即使catch又出现异常了,或者try中有return语句,catch中有return语句的情况
像数据库连接、输入输出流、网络编程Stock等资源、JVM是不能自动回收的,我们需要自己进行手动资源的释放,这需要声明在finally中
package baozhuang;
import org.junit.Test;
public class finallyTest {
@Test
public void test1(){
try{
int a = 10;
int b = 0;
System.out.println(a/b);
}catch(ArithmeticException e){
//e.printStackTrace();
int[] arr = new int[10];
System.out.println(arr[10]); // 异常
}catch(Exception e){
e.printStackTrace();
}finally{
System.out.println("我好帅"); // 照样执行
}
System.out.println("我好帅222"); // 不执行
}
}
package baozhuang;
import java.io.FileNotFoundException;
import java.io.IOException;
public class OverrideTest {
public static void main(String[] args) {
OverrideTest test = new OverrideTest();
test.display(new SubClass());
}
public void display(SuperClass s){
try{
s.method();
}catch(IOException e){
e.printStackTrace();
}
}
}
class SuperClass{
public void method() throws IOException{
}
}
class SubClass extends SuperClass{
public void method() throws FileNotFoundException{
}
}
package baozhuang;
public class StudentTest {
public static void main(String[] args) {
try {
Student s = new Student();
s.regist(-1001);
System.out.println(s);
} catch (Exception e) {
// e.printStackTrace();
System.out.println(e.getMessage());
}
}
}
class Student{
private int id;
public void regist(int id) throws Exception{
if (id > 0){
this.id = id;
}else{
//System.out.println("输入的id违法");
// 手动抛出异常对象
//throw new RuntimeException("您输入的数据违法!");
throw new Exception("您输入的数据违法!");
}
}
@Override
public String toString() {
return "Student [id=" + id + "]";
}
}
1.需要去继承现有的异常类,例如RuntimeException,Exception
2. 提供seriaVersionUID
3. 提供重载的构造器
package baozhuang;
public class MyException extends RuntimeException {
static final long seriaVersionUID = -7034897190745766939L;
public MyException(){
}
public MyException(String msg){
super(msg);
}
}