java 异常处理方法
- private String assembleErrorMessage(Exception e) {
- JSONObject errorMessage = new JSONObject();
- Optional<StackTraceElement> stackTraceElement = Arrays.stream(e.getStackTrace()).findFirst();
- String cause = e.toString();
- String className = stackTraceElement.map(stackTraceElement1 -> stackTraceElement.get().getClassName()).orElse("");
- String fileName = stackTraceElement.map(stackTraceElement1 -> stackTraceElement.get().getFileName()).orElse("");
- String methodName = stackTraceElement.map(stackTraceElement1 -> stackTraceElement.get().getMethodName()).orElse("");
- int lineNumber = stackTraceElement.map(stackTraceElement1 -> stackTraceElement.get().getLineNumber()).orElse(0);
- errorMessage.put("cause", cause);
- errorMessage.put("className", className);
- errorMessage.put("fileName", fileName);
- errorMessage.put("methodName", methodName);
- errorMessage.put("lineNumber", lineNumber);
- return errorMessage.toString();
- }
效果
原文链接:https://blog.csdn.net/qq_45469347/article/details/126487683 标签:stackTraceElement1,map,java,String,处理,errorMessage,stackTraceElement,put,异常 From: https://www.cnblogs.com/sunny3158/p/18241718