1、exception对象概述
该对象的作用是显示异常信息。exception 对象的作用是显示异常信息,只有在包含 isErrorPage=”true” 的页面中才可以被使用,在一般的JSP页面中使用该对象将无法编译JSP文件。
excepation对象和Java的所有对象一样,都具有系统提供的继承 结构。exception对象是java.lang.Throwable的对象。
exception 对象几乎定义了所有异常情况。在Java程序中,可以使用try/catch关键字来处理异常情况; 如果在JSP页面中出现没有捕获到的异常,就会生成 exception 对象,并把 exception 对象传送到在page指令中设定的错误页面中,然后在错误页面中处理相应的 exception 对象。常用的方法如下:
方法 | 说明 |
---|---|
getMessage() | 返回异常对象的异常信息字符串 |
getLocalizedmessage() | 返回本地化的异常错误 |
toString() | 将异常信息的简单描述 |
fillInstackTrace | 重写异常错误的栈执行轨迹 |
index.jsp:
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
int apple = Integer.parseInt("ad");
//int apple = Integer.parseInt("3");
out.println("苹果每斤"+apple+"元");
%>
</body>
</html>
error.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk" isErrorPage="true"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'error.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
错误提示为:<%=exception.getMessage() %>
</body>
</html>