Here you can find the source of getCause(Throwable throwable)
public static Throwable getCause(Throwable throwable)
//package com.java2s; //License from project: Apache License import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.sql.SQLException; public class Main { protected static String[] CAUSE_METHOD_NAMES = { "getCause", "getNextException", "getTargetException", "getException", "getSourceException", "getRootCause", "getCausedByException", "getNested" }; public static Throwable getCause(Throwable throwable) { return getCause(throwable, CAUSE_METHOD_NAMES); }/* w w w. j a va2 s . co m*/ public static Throwable getCause(Throwable throwable, String[] methodNames) { Throwable cause = getCauseUsingWellKnownTypes(throwable); if (cause == null) { for (String methodName : methodNames) { cause = getCauseUsingMethodName(throwable, methodName); if (cause != null) { break; } } if (cause == null) { cause = getCauseUsingFieldName(throwable, "detail"); } } return cause; } private static Throwable getCauseUsingWellKnownTypes(Throwable throwable) { if (throwable instanceof SQLException) { return ((SQLException) throwable).getNextException(); } else if (throwable instanceof InvocationTargetException) { return ((InvocationTargetException) throwable).getTargetException(); } else { return null; } } private static Throwable getCauseUsingMethodName(Throwable throwable, String methodName) { Method method = null; try { method = throwable.getClass().getMethod(methodName, null); } catch (NoSuchMethodException ignored) { } catch (SecurityException ignored) { } if (method != null && Throwable.class.isAssignableFrom(method.getReturnType())) { try { return (Throwable) method.invoke(throwable, new Object[0]); } catch (IllegalAccessException ignored) { } catch (IllegalArgumentException ignored) { } catch (InvocationTargetException ignored) { } } return null; } private static Throwable getCauseUsingFieldName(Throwable throwable, String fieldName) { Field field = null; try { field = throwable.getClass().getField(fieldName); } catch (NoSuchFieldException ignored) { } catch (SecurityException ignored) { } if (field != null && Throwable.class.isAssignableFrom(field.getType())) { try { return (Throwable) field.get(throwable); } catch (IllegalAccessException ignored) { } catch (IllegalArgumentException ignored) { } } return null; } }