Java >> Tutoriel Java >  >> Java

Différentes façons d'imprimer un message d'exception en Java

Chaque fois qu'une exception est lancée, Classe jetable fournit diverses méthodes pour fournir des informations relatives aux exceptions comme le nom de l'exception, la description de l'exception et la trace de la pile, etc.

Nous discuterons de trois méthodes de la classe Throwable qui fournit des informations relatives aux exceptions, donc le nom de ces méthodes est :

  1. méthode printStackTrace()
  2. méthode toString()
  3. méthode getMessage()

Nous allons voir à quoi servent ces méthodes et comment cela fonctionne...

1) méthode printStackTrace()

  • Cette méthode est disponible dans le package java.lang.Throwable.printStackTrace().
  • Cette méthode fournit des informations relatives aux exceptions et nous verrons quelles informations cette méthode fournira.
    • Nom de l'exception
    • Description de l'exception
    • Suivi de pile de l'exception

Syntaxe :

    Name of the Exception : Description of the Exception
    Stack Trace of the Exception

Exemple :

class PrintStackTrace {

    public static void main(String[] args) {

        Object obj = null;

        try {
            System.out.println(obj.toString());
        } catch (Exception ex) {
            /*Display exception name : exception description
            Stack trace */
            ex.printStackTrace();
        }

    }

}

Sortie

E:\Programs>javac PrintStackTrace.java
E:\Programs>java PrintStackTrace
java.lang.NullPointerException
        at PrintStackTrace.main(PrintStackTrace.java:8)

2) méthode toString()

  • Cette méthode est disponible dans le package java.lang.Throwable.toString().
  • Cette méthode fournit également des informations relatives aux exceptions et nous verrons à nouveau quelles informations cette méthode fournira.
    • Nom de l'exception
    • Description de l'exception

Syntaxe :

    Name of the Exception : Description of the Exception

Exemple :

class ToStringMethod {

    public static void main(String[] args) {

        try {
            int i = 10 / 0;
            System.out.println(i);
        } catch (Exception ex) {
            // Display exception name : exception description		
            System.out.println(ex.toString());
        }

    }

}

Sortie

E:\Programs>javac ToStringMethod.java

E:\Programs>java ToStringMethod
java.lang.ArithmeticException: / by zero

3) méthode getMessage()

  • Cette méthode est également disponible dans le package java.lang.Throwable.printStackTrace().
  • Cette méthode fournit des informations relatives à l'exception et nous verrons quelles informations cette méthode fournira.
    Description de l'exception
  • Cette méthode ne fournit pas d'autres informations telles que le nom de l'exception et la trace de la pile d'exceptions.

Syntaxe :

    Description of the Exception

Exemple :

class GetMessageMethod {

    public static void main(String[] args) {

        try {
            int i = 10 / 0;
            System.out.println(i);
        } catch (Exception ex) {
            // Display exception description
            System.out.println(ex.getMessage());
        }

    }

}

Sortie

E:\Programs>javac GetMessageMethod.java

E:\Programs>java GetMessageMethod
/ by zero

Balise Java