异常
public class Test01
{
	public static void main(String[] args){
		int i=10,j=0;//java.lang.ArithmeticException:
		System.out.println(i/j);
		System.out.println("程序执行结束");
	}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
        at Test01.main(Test01.java:5) 
import java.io.*;
public class Test02
{
	public static void main(String[] args) throws FileNotFoundException{
		getIO();
		System.out.println("程序执行结束");
	}
	public static void getIO() throws FileNotFoundException{
		FileInputStream fis=new FileInputStream("d:/a.txt");//java.io.FileNotFoundException:
	}
}
Exception in thread "main" java.io.FileNotFoundException: d:\a.txt (系统找不到指定的文件。
)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:146)
        at java.io.FileInputStream.<init>(FileInputStream.java:101)
        at Test02.getIO(Test02.java:9)
        at Test02.main(Test02.java:5)
try{
java语句;
}catch(匹配异常类型){
执行输出异常信息
}
后续代码
try块中带有多个catch时的主意事项
如果多个catch之间的异常类型之间有继承关系,则先字后父。
import java.io.*;
public class Test03
{
	public static void main(String[] args) {
		getIO();
		System.out.println("程序执行结束");
	}
	public static void getIO() {
		try{
			FileInputStream fis=new FileInputStream("d:/a.txt");//java.io.FileNotFoundException:
		}catch(FileNotFoundException f){
			f.getMessage();
			f.printStackTrace();
		}
	}
}
java.io.FileNotFoundException: d:\a.txt (系统找不到指定的文件。)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:146)
        at java.io.FileInputStream.<init>(FileInputStream.java:101)
        at Test03.getIO(Test03.java:10)
        at Test03.main(Test03.java:5)
程序执行结束
import java.io.*;
public class Test04
{
	public static void main(String[] args) {
		getIO();
		System.out.println("程序执行结束");
	}
	public static void getIO() {
		try{
			FileInputStream fis=new FileInputStream("d:/a.txt");//java.io.FileNotFoundException:
		}catch(FileNotFoundException f){
			f.printStackTrace();
		}finally{
			System.out.println("catch后续代码");
		}
	}
}
java.io.FileNotFoundException: d:\a.txt (系统找不到指定的文件。)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:146)
        at java.io.FileInputStream.<init>(FileInputStream.java:101)
        at Test04.getIO(Test04.java:10)
        at Test04.main(Test04.java:5)
catch后续代码
程序执行结束 
import java.io.*;
public class Test04
{
	public static void main(String[] args) {
		getIO();
		System.out.println("程序执行结束");
	}
	public static void getIO() {
		try{
			FileInputStream fis=new FileInputStream("d:/a.txt");//java.io.FileNotFoundException:
		}catch(FileNotFoundException f){
			f.printStackTrace();
			System.exit(1);
		}finally{
			System.out.println("catch后续代码执行");
		}
	}
}
java.io.FileNotFoundException: d:\a.txt (系统找不到指定的文件。)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:146)
        at java.io.FileInputStream.<init>(FileInputStream.java:101)
        at Test04.getIO(Test04.java:10)
        at Test04.main(Test04.java:5) 
final是一个关键字修饰类不能被继承修饰方法不能被覆盖修饰变量一旦值赋值后不能改变值 和static final 变量变常量 ,比如static final PI;
finally是异常机制,在try catch后加上则finally中的代码一定会被执行 除了System.exit(1);
finalize是一个方法名 是垃圾回收机制在清理前调用对象执行。
throws 加异常类型 throws FileNotFoundException
throw new 异常类型(可以是自定义类型)
public class Test05
{
	public static void main(String[] args) throws AgeException{
		Student  s=new Student();
		s.setAge(-9);
		System.out.println(s.getAge());
	}
}
public class AgeException extends Exception
{
	public AgeException(){
		super();
	}
	public AgeException(String message){
		super(message);
	}
}
public class Student 
{
	private int age;
	public int getAge(){
		return age;
	}
	public void setAge(int age) throws AgeException{
		if(age >0 && age <100){
			this.age=age;
		}else{
			throw new AgeException("年龄必须在0到100岁之间");
		}
	}
}
Exception in thread "main" AgeException: 年龄必须在0到100岁之间
        at Student.setAge(Student.java:12)
        at Test05.main(Test05.java:5)                          rosoft YaHei UI RegularÌÌÌÌÌÌÌࠀ빜걠ꛁ3ꛁ3Stream.<ini
                    
                
                
            
        
浙公网安备 33010602011771号