12_Java_Exception-Behandlung und Eingabe und Ausgabe

abnormal

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

try fügt mögliche Ausnahmen ein und catch behandelt mögliche Ausnahmen;

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*/
Ausnahme abfangen

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
*/
gefangene Ausnahme

Nach Erhalt der Ausnahme:

  • Zeichenfolge 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 
*/
wieder werfen
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
*/

Ausnahmemechanismus

Der größte Vorteil des Ausnahmemechanismus besteht darin, dass er den normalen Geschäftslogikcode und den Verarbeitungscode beim Auftreten von Situationen klar voneinander trennt;

Auslösen und Deklarieren von Ausnahmen

Ausnahmedeklaration: Wenn die Funktion eine Ausnahme auslösen darf, muss sie im Funktionsheader deklariert werden;

Die Ausnahmeklasse erbt Throwable

  • werfe neue Exceptionin()
  • throw new Exception („Hilfe“)
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();
		}
	}
}
Übereinstimmung, wenn eine Ausnahme abgefangen wird

Die von der Unterklasse ausgelöste Ausnahme wird von dem Catch abgefangen, der die Ausnahme der übergeordneten Klasse abfängt;

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)
*/

Alle Ausnahmen erben von Exception, die wiederum von Throwable erbt;
jede Ausnahme abfangen:

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)
*/
Laufzeitausnahme
  • Ausnahmen wie ArrayIndexOutOfBoundsException müssen nicht deklariert werden
  • Aber wenn es keinen geeigneten Mechanismus gibt, um es abzufangen, wird es schließlich dazu führen, dass das Programm beendet wird
Ausnahme bei Vererbung aufgetreten
  • Wenn die Funktion eine Ausnahme auslösen darf, muss sie im Funktionsheader deklariert werden
  • Wenn eine zum Auslösen einer Ausnahme deklarierte Funktion aufgerufen wird, muss sie:
    • Setzen Sie den Funktionsaufruf in einen try-Block und setzen Sie catch, um alle Ausnahmen abzufangen, die ausgelöst werden können
    • oder deklarieren Sie sich selbst, um eine unbehandelte Ausnahme auszulösen

Notiz:

  • Beim Überschreiben einer Funktion können Unterklassen nicht deklarieren, dass sie mehr Ausnahmen auslösen als die Version der Oberklasse
  • Im Konstruktor der Unterklasse müssen alle Ausnahmen deklariert werden, die von der Oberklasse geworfen werden können

Fluss

Ein Stream ist eine Art der Ein- und Ausgabe. Ein
Stream ist eindimensional

Ich denke du magst

Origin blog.csdn.net/qq_45459526/article/details/122936446
Empfohlen
Rangfolge