上一篇
java 页面怎么获取throw new
- 后端开发
- 2025-07-28
- 9
Java页面中,可以通过捕获异常来获取
throw new
抛出的异常信息,使用try-catch块捕获并处理异常,然后通过e.
Java Web开发中,页面获取throw new
异常信息通常涉及到后端异常处理和前端展示,以下是详细的步骤和方法,帮助你在Java页面中捕获并展示throw new
抛出的异常信息。
理解throw new
的作用
throw new
用于在Java代码中主动抛出异常。
throw new IllegalArgumentException("参数不合法");
这行代码会立即终止当前方法的执行,并将异常抛给调用者处理。
后端异常处理
为了在页面上展示异常信息,首先需要在后端捕获并处理这些异常,常见的做法包括使用全局异常处理器或在每个方法中进行异常捕获。
1 使用全局异常处理器(Spring Boot示例)
如果你使用的是Spring Boot,可以通过@ControllerAdvice
和@ExceptionHandler
来全局处理异常。
import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e) { // 记录日志 System.err.println("捕获到异常: " + e.getMessage()); // 返回错误信息给前端 return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); } }
2 在控制器中捕获异常
如果不使用全局异常处理器,可以在每个控制器方法中进行异常捕获。
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/test") public String test() { try { // 可能抛出异常的代码 throw new IllegalArgumentException("参数不合法"); } catch (Exception e) { // 捕获异常并返回错误信息 return "错误: " + e.getMessage(); } } }
前端展示异常信息
前端需要接收后端返回的错误信息并进行展示,以下以JavaScript和HTML为例。
1 使用AJAX请求
假设你使用AJAX向后端发送请求并处理响应。
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8">异常处理示例</title> </head> <body> <button id="testBtn">测试异常</button> <div id="result"></div> <script> document.getElementById('testBtn').addEventListener('click', function() { fetch('/test') .then(response => { if (!response.ok) { return response.text().then(text => { throw new Error(text) }); } return response.text(); }) .then(data => { document.getElementById('result').innerText = data; }) .catch(error => { document.getElementById('result').innerText = '捕获到异常: ' + error.message; }); }); </script> </body> </html>
2 使用Fetch API
上述代码使用了Fetch API来发送请求并处理响应,如果响应状态码不是200,则读取响应体并将其作为错误抛出,然后在catch
块中捕获并展示错误信息。
结合表格展示异常信息
你可以将异常信息以表格形式展示在页面上,便于用户查看。
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8">异常处理示例</title> <style> table { border-collapse: collapse; width: 50%; margin: 20px auto; } th, td { border: 1px solid #ddd; padding: 8px; } th { background-color: #f2f2f2; } </style> </head> <body> <button id="testBtn">测试异常</button> <table id="errorTable"> <thead> <tr> <th>错误类型</th> <th>错误信息</th> </tr> </thead> <tbody></tbody> </table> <script> document.getElementById('testBtn').addEventListener('click', function() { fetch('/test') .then(response => { if (!response.ok) { return response.text().then(text => { throw new Error(text) }); } return response.text(); }) .then(data => { const tableBody = document.querySelector('#errorTable tbody'); tableBody.innerHTML = ''; // 清空现有内容 const row = tableBody.insertRow(); const cellType = row.insertCell(0); const cellMessage = row.insertCell(1); cellType.textContent = '成功'; cellMessage.textContent = data; }) .catch(error => { const tableBody = document.querySelector('#errorTable tbody'); tableBody.innerHTML = ''; // 清空现有内容 const row = tableBody.insertRow(); const cellType = row.insertCell(0); const cellMessage = row.insertCell(1); cellType.textContent = '错误'; cellMessage.textContent = error.message; }); }); </script> </body> </html>
相关问答FAQs
Q1: 如何在Spring Boot中自定义异常响应?
A1: 在Spring Boot中,你可以通过@ControllerAdvice
和@ExceptionHandler
来自定义异常响应,可以返回一个JSON对象包含错误码和错误信息,或者根据不同的异常类型返回不同的HTTP状态码。
Q2: 前端如何处理多个异常类型?
A2: 前端可以根据捕获到的异常类型进行不同的处理,在catch
块中判断error.name
或`error.