Ví dụ Java về Exception trong Python



Miêu tả vấn đề

Cách xử lý Exception Method trong Java?

Giải pháp

Ví dụ sau minh họa cách xử lý Exception Methods bởi sử dụng phương thức System.err.println() của lớp System trong Java.

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();
       }
 }

Kết quả

Code trên sẽ cho kết quả sau:

Caught Exception
getMassage(): My Exception
gatLocalisedMessage(): My Exception
toString():java.lang.Exception: My Exception
print StackTrace():
 java.lang.Exception: My Exception
   at ExceptionMethods.main(ExceptionMeyhods.java:12)

exception_trong_java.jsp