zoukankan      html  css  js  c++  java
  • 12.exception对象

    excepton对象是一个异常对象,当一个页面在运行过程中发生了异常,就产生了这个对象,如果一个jsp页面要应用此对象,就必须把isErrorPage设置为true,否则无法编译。它实际上是java.lang.Throwable的对象,常用方法:

    String getMessage()返回描述异常的消息

    String toString()反水关于异常的简短描述信息

    void printStackTrace()显示异常及其栈轨迹

    Throwable FillInStackTrace()重写异常的执行栈轨迹

    使用:在实际中有时候会出现异常,可以专门写一个jsp来处理

    抛出异常的jsp

    <%@ page language="java" import="java.util.*"
        contentType="text/html; charset=utf-8" errorPage="exception.jsp"%>
    <%
        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 'login.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>
        <h1>抛出异常页面</h1>
        <%
        System.out.println(100/0);
        %>
    </body>
    </html>

    需要在page头那里添加处理异常的jsp,即errorPage="exception.jsp"

    exception.jsp

    <%@ page language="java" import="java.util.*"
        contentType="text/html; charset=utf-8" 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 'login.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>
        <h1>exception内置对象</h1>
        异常消息为:<%=exception.getMessage() %><br>
        异常的字符串描述:<%=exception.toString() %>
    </body>
    </html>

    需要在头部添加

    isErrorPage="true"

    运行的结果

    
    



  • 相关阅读:
    hdu 2199 Can you solve this equation? 二分
    STL 学习代码~
    hdu 1551 Cable master 二分
    fzu 1564 Combination 组合数是否包含因数
    fafu 1079 帮冬儿忙 组合数包含因数个数
    soj 3290 Distribute The Apples I 组合数对素数取余
    fzu 2020 组合 组合数对素数取余
    hdu 1969 Pie 二分
    hdu 2141 Can you find it? 二分
    hdu 2899 Strange fuction 二分
  • 原文地址:https://www.cnblogs.com/caimuqing/p/5776599.html
Copyright © 2011-2022 走看看