Ejemplos de Java - Métodos de manejo de excepciones

Java 实例Ejemplos de Java

El siguiente ejemplo demuestra el uso de System.err.println() de la clase System para mostrar el manejo de excepciones:

Archivo ExceptionDemo.java

class ExceptionDemo { public static void main(String[] args) { try { throw new Exception("My Exception"); } catch (Exception e) { System.err.println("Caught Exception"); System.err.println("getMessage():" + e.getMessage()); System.err.println("getLocalizedMessage():" + e.getLocalizedMessage()); System.err.println("toString():" + e); System.err.println("printStackTrace():"); e.printStackTrace(); } } }

La salida del código anterior es:

Caught Exception
getMessage():My Exception
getLocalizedMessage():My Exception
toString():java.lang.Exception: My Exception
printStackTrace():
java.lang.Exception: My Exception
   at ExceptionDemo.main(ExceptionDemo.java:5)

Java 实例Ejemplos de Java

Otras extensiones