Heim  >  Artikel  >  Java  >  Was sind die Schlüsselwörter der Java-Ausnahmebehandlung?

Was sind die Schlüsselwörter der Java-Ausnahmebehandlung?

王林
王林Original
2020-05-13 13:31:503777Durchsuche

Was sind die Schlüsselwörter der Java-Ausnahmebehandlung?

Was ist eine Ausnahme?

Einige Ausnahmen werden durch Benutzerfehler verursacht, andere durch Programmfehler und andere durch physische Fehler.

Schlüsselwörter für die Ausnahmebehandlung:

versuchen, fangen, schließlich werfen, wirft

Hinweise:

1. Fehler sind keine Ausnahmen, sondern Escapes der Programmiersteuerung.

2. Alle Ausnahmeklassen sind Unterklassen, die von der Klasse java.lang.Exception geerbt wurden.

3. Die Ausnahmeklasse hat zwei Hauptunterklassen: die IOException-Klasse und die RuntimeException-Klasse.

4. Java verfügt über viele integrierte Ausnahmeklassen.

(Empfohlenes Video-Tutorial: Java-Video )

Syntax:

try{
//需要监听的代码块
}
catch(异常类型 异常名称/e){
//对捕获到try监听到的出错的代码块进行处理
throw 异常名称/e; //thorw表示抛出异常
throw new 异常类型(“自定义”);
}
finally{
//finally块里的语句不管异常是否出现,都会被执行
}
修饰符 返回值 方法名 () throws 异常类型{  
//throws只是用来声明异常,是否抛出由方法调用者决定
//代码块
}

Beispielcode: (versuchen vs. fangen vs. endlich)

public class ExceptionTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);  
        try{ //监听代码块  
        int a=input.nextInt();  
        int b=input.nextInt();  
        double sum=a/b;   
        System.out.println(sum);  
        }  
        catch(InputMismatchException e){  
            System.out.println("只能输入数字");  
        }  
        catch(ArithmeticException e){  
            System.out.println("分母不能为0");  
        }  
        catch(Exception e){ //Exception是所有异常的父类  
            System.out.println("发生了其他异常");  
        }  
        finally{ //不管是否出现异常,finally一定会被执行  
            System.out.println("程序结束");  
        }  
	}
}

Beispielcode: (Schlüsselwort werfen)

import java.util.InputMismatchException;
import java.util.Scanner;
 
public class ExceptionTest {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);  
        try{ //监听代码块  
        int a=input.nextInt();  
        int b=input.nextInt();  
        double sum=a/b;   
        System.out.println(sum);  
        }  
        catch(InputMismatchException e){ //catch(异常类型 异常名称)  
            System.out.println("只能输入数字");  
            throw e; //抛出catch捕捉到的异常  
            //throw new InputMismatchException(); 同上  
        }  
        catch(ArithmeticException e){  
            System.out.println("分母不能为0");  
            throw new ArithmeticException("分母为0抛出异常"); //抛出ArithmeticException异常  
        }  
        catch(Exception e){ //Exception是所有异常的父类  
            System.out.println("发生了其他异常");  
        }  
        finally{ //不管是否出现异常,finally一定会被执行  
            System.out.println("程序结束");  
        }   
	}
}

Beispielcode: (wirft)

public class Throws {
	int a=1;
	int b=0;
	public void out() throws ArithmeticException{ //声明可能要抛出的异常,可以有多个异常,逗号隔开
		try{ //监听代码块
		int sum=a/b;
		System.out.println(sum);
		}
		catch(ArithmeticException e){
			System.out.println("分母不能为0");
		}
		finally{ //不管是否出现异常,finally一定会被执行
			System.out.println("程序结束");
		}
	}
	public static void main(String[] args){
		Throws t=new Throws();
			t.out(); //调用方法
			throw new ArithmeticException("分母为0抛出异常"); //由调用的方法决定是否要抛出异常
			/*
			 * 第二种抛出方式
			 */
//			ArithmeticException a=new ArithmeticException("分母为0抛出异常");
//			throw a;
	}
}

Empfohlenes Tutorial: Java-Eingabeprogramm

Das obige ist der detaillierte Inhalt vonWas sind die Schlüsselwörter der Java-Ausnahmebehandlung?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn