Java >> Tutoriel Java >  >> Java

Est-il nécessaire que chaque bloc try soit suivi d'un bloc catch en Java ?

La question est que "Est-il nécessaire que chaque bloc try soit suivi d'un bloc catch en Java ?"

La réponse est "Non, il n'est pas obligatoire que chaque bloc try soit suivi d'un bloc catch en Java."

  • Après le bloc try, nous pouvons utiliser le bloc "catch" ou le bloc "finally".
  • Généralement, les exceptions levées doivent être déclarées dans la clause levée de la méthode.
  • Pour comprendre le bloc try-catch, nous aborderons trois cas :
    1. Que se passera-t-il si chaque bloc try doit être suivi d'un bloc catch ?
    2. Que se passera-t-il si chaque bloc try doit être suivi d'un bloc finally ?
    3. Que se passera-t-il si chaque bloc try doit être suivi à la fois d'un bloc catch et d'un bloc finally ?

Dans les quelques étapes, nous allons explorer chacun des cas ci-dessus un par un à l'aide d'un exemple,

1) Chaque bloc try est suivi d'un bloc catch

Exemple :

// Java program to demonstrate the example of
// try-catch block hierarchy 

public class TryCatchBlock {
    public static void main(String[] args) {

        try {
            int i1 = 10;
            int i2 = 0;
            
            int result = i1 / i2;
            
            System.out.println("The divison of i1,i2 is" + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

Sortie

java.lang.ArithmeticException: / by zero
        at TryCatchBlock.main(TryCatchBlock.java:8)

2) Chaque bloc try est suivi d'un bloc finally

Exemple :

// Java program to demonstrate the example of
// try-finally block hierarchy 	

public class TryFinallyBlock {
    public static void main(String[] args) {
        
        try {
            int i1 = 10;
            int i2 = 0;
            
            int result = i1 / i2;
            
            System.out.println("The divison of i1,i2 is" + result);
        } finally {
            System.out.print("Code which must be executed :" + " ");
            System.out.println("Whether Exception throw or not throw");
        }
        
    }
}

Sortie

Code which must be executed : Whether Exception throw or not throw

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at TryFinallyBlock.main(TryFinallyBlock.java:11)

3) Chaque bloc try est suivi à la fois d'un bloc catch et d'un bloc finally

Exemple :

// Java program to demonstrate the example of
// try-catch-finally block hierarchy 

public class TryCatchFinallyBlock {
    public static void main(String[] args) {
        int i1 = 10;
        int i2 = 0;
        
        try {
            int result = i1 / i2;
        
            System.out.println("The divison of i1,i2 is" + result);
        } catch (Exception ex) {
            int result = i1 + i2;
            System.out.println("The addition of i1,i2 is" + " " + result);
        } finally {
            System.out.print("Code which must be executed :" + " ");
            System.out.println("Whether Exception throw or not throw");
        }
    }
}

Sortie

The addition of i1,i2 is 10
Code which must be executed : Whether Exception throw or not throw

Les combinaisons try, catch et finally données ci-dessous sont valables et nous l'avons vu à l'aide d'un exemple donné ci-dessus,

  • bloc try-catch
  • bloquer try-catch-finally
  • bloquer try-finally

Balise Java