12_Java_异常处理与输入输出

异常

try{
    
    
……
} catch(ArrayIndexOutOfBoundsException e){
    
    
……
}

try 放的是可能发生的异常,catch 对可能发生的异常做处理;

package hello;

import java.util.Scanner;

public class Main{
    
    
	
	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		int idx = in.nextInt();
		int[] a = new int[10];//下标最大为9
		try {
    
    
			a[idx] = 10;
			System.out.println("Hello");
		} catch(ArrayIndexOutOfBoundsException e) {
    
    
			System.out.println("Catch");
		}
	}
}
/*1 Hello*/
/*10 Catch*/
捕捉异常

package hello;

import java.util.Scanner;

public class Main{
    
    
	public static void f()
	{
    
    
		int[] a = new int[10];
		a[10] = 10;
		System.out.println("JHello");
	}
	public static void main(String[] args) {
    
    
		try {
    
    
			f();
		} catch(ArrayIndexOutOfBoundsException e) {
    
    
			System.out.println("catch");
		}
		System.out.println("main");
	}
}
/*
catch
main
*/

package hello;

import java.util.Scanner;

public class Main{
    
    
	public static void f()
	{
    
    
		int[] a = new int[10];
		a[10] = 10;
		System.out.println("JHello");
	}
	public static void k()
	{
    
    
		f();
	}
	public static void h()
	{
    
    
		int num = 10;
		if(num < 100)
		{
    
    
			k();
		}
	}
	public static void p()
	{
    
    
		try {
    
    
			h();
		} catch (NullPointerException e) {
    
    
			System.out.println("p()");
		}
	}
	public static void main(String[] args) {
    
    
		try {
    
    
			p();
		} catch(ArrayIndexOutOfBoundsException e) {
    
    
			System.out.println("catch");
		}
		System.out.println("main");
	}
}
/*
catch
main
*/
捕捉到的异常

拿到异常之后:

  • String getMessage()
  • String tostring()
  • void printStackTrace()
package hello;

import java.util.Scanner;

public class Main{
    
    
	public static void f()
	{
    
    
		int[] a = new int[10];
		a[10] = 10;
		System.out.println("JHello");
	}
	public static void k()
	{
    
    
		f();
	}
	public static void h()
	{
    
    
		int num = 10;
		if(num < 100)
		{
    
    
			k();
		}
	}
	public static void p()
	{
    
    
		try {
    
    
			h();
		} catch (NullPointerException e) {
    
    
			System.out.println("p()");
		}
	}
	public static void main(String[] args) {
    
    
		try {
    
    
			p();
		} catch(ArrayIndexOutOfBoundsException e) {
    
    
			System.out.println("catch");
			System.out.println(e.getMessage());
			System.out.println(e);
			e.printStackTrace();
		}
		System.out.println("main");
	}
}
/*
catch
Index 10 out of bounds for length 10
java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
	at hello.Main.f(Main.java:9)
	at hello.Main.k(Main.java:14)
	at hello.Main.h(Main.java:21)
	at hello.Main.p(Main.java:27)
	at hello.Main.main(Main.java:34)
main 
*/
再度抛出
package hello;

import java.util.Scanner;

public class Main{
    
    
	public static void f()
	{
    
    
		int[] a = new int[10];
		a[10] = 10;
		System.out.println("JHello");
	}
	public static void k()
	{
    
    
		f();
	}
	public static void h()
	{
    
    
		int num = 10;
		if(num < 100)
		{
    
    
			k();
		}
	}
	public static void p()
	{
    
    
		try {
    
    
			h();
		} catch (ArrayIndexOutOfBoundsException e) {
    
    
			System.out.println("p()");
		}
	}
	public static void main(String[] args) {
    
    
		try {
    
    
			p();
		} catch(ArrayIndexOutOfBoundsException e) {
    
    
			System.out.println("catch");
		}
		System.out.println("main");
	}
}
/*
p()
main
*/
package hello;

import java.util.Scanner;

public class Main{
    
    
	public static void f()
	{
    
    
		int[] a = new int[10];
		a[10] = 10;
		System.out.println("JHello");
	}
	public static void k()
	{
    
    
		f();
	}
	public static void h()
	{
    
    
		int num = 10;
		if(num < 100)
		{
    
    
			k();
		}
	}
	public static void p()
	{
    
    
		try {
    
    
			h();
		} catch (ArrayIndexOutOfBoundsException e) {
    
    
			System.out.println("p()");
			throw e;
		}
	}
	public static void main(String[] args) {
    
    
		try {
    
    
			p();
		} catch(ArrayIndexOutOfBoundsException e) {
    
    
			System.out.println("catch");
		}
		System.out.println("main");
	}
}
/*
p()
catch
main
*/

异常机制

异常机制最大的好处就是清晰的分开了正常的业务逻辑代码和遇到情况时的处理代码;

异常的抛出与声明

异常声明:如果函数可能抛出异常,就必须在函数头部加声明;

Exception类继承了 Throwable

  • throw new Exceptin()
  • throw new Exception(“Help”)
package hello;

import java.util.Scanner;

class openException extends Throwable{
    
    
	
}
class closeException extends Throwable{
    
    
	
}
public class Main{
    
    
	public static int open()
	{
    
    
		return -1;
	}
	public static void readfile() throws openException, closeException
	{
    
    
		if(open() ==-1)
		{
    
    
			throw new openException();
		}
	}
	public static void main(String[] args) {
    
    
		try {
    
    
			readfile();
		} catch (openException e) {
    
    
			// TODO Auto-generated catch block
			System.out.println(e);
			e.printStackTrace();
		} catch (closeException e) {
    
    
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
异常捕捉时的匹配

抛出子类的异常会被捕捉父类异常的catch给捉到;

package hello;

import java.util.Scanner;

class openException extends Exception{
    
    
	
}
class closeException extends openException{
    
    
	
}
public class Main{
    
    
	public static int open()
	{
    
    
		return -1;
	}
	public static void readfile() throws openException, closeException
	{
    
    
		if(open() ==-1)
		{
    
    
			throw new closeException();
		}
	}
	public static void main(String[] args) {
    
    
		try {
    
    
			readfile();
		} catch (openException e) {
    
    
			// TODO Auto-generated catch block
			System.out.println(e);
			e.printStackTrace();
		}
	}
}
/*
hello.closeException
hello.closeException
	at hello.Main.readfile(Main.java:20)
	at hello.Main.main(Main.java:25)
*/

所有的异常都继承自Exception,而Exception继承自Throwable;
捕捉任何异常:

catch(Exception e){
    
    
……
}
package hello;

import java.util.Scanner;

class openException extends Exception{
    
    
	
}
class closeException extends openException{
    
    
	
}
public class Main{
    
    
	public static int open()
	{
    
    
		int[] a = new int[10];
		
		a[10] = 10;
		return -1;
	}
	public static void readfile() throws openException, closeException
	{
    
    
		if(open() ==-1)
		{
    
    
			throw new closeException();
		}
	}
	public static void main(String[] args) {
    
    
		try {
    
    
			readfile();
		} catch (openException e) {
    
    
			// TODO Auto-generated catch block
			System.out.println(e);
			e.printStackTrace();
		} catch (Exception e)
		{
    
    
			e.printStackTrace();
		}
	}
}
/*
java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
	at hello.Main.open(Main.java:16)
	at hello.Main.readfile(Main.java:21)
	at hello.Main.main(Main.java:28)
*/
运行时刻异常
  • 像ArrayIndexOutOfBoundsException这样的异常是不需要声明的
  • 但是如果没有适当的机制来捕捉,最终会导致程序终止
异常遇到继承
  • 如果函数可能抛出异常,就必须在函数头部加以声明
  • 如果调用了一个声明会抛出异常的函数,那必须:
    • 把函数的调用放在try块中,并设置catch来捕捉所有可抛出的异常
    • 或声明自己会抛出无法处理的异常

注意:

  • 当覆盖一个函数的时候,子类不能声明抛出比父类的版本更多的异常
  • 在子类的构造函数中,必须声明父类可能抛出的全部异常

流是输入输出的方式
流是一维的

猜你喜欢

转载自blog.csdn.net/qq_45459526/article/details/122936446