The following example demonstrates the use of System.err.println() of the System class to display the exception handling method:
/* author by w3cschool.cc 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(); } } }
The output result of running the above code is:
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)
Above It is the content of Java Example - Exception Handling Method. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!