我想要的效果是这样的: 错误码: 500 讯息: Unable to compile class for JSP An error occurred at line: 3 in the jsp file: /sam/test/manage/error.jsp Generated servlet error: [javac] Compiling 1 source file D:\Sun\AppServer\domains\jakarta-tomcat-4.1.30\work\Standalone\localhost\_\sam\test\manage\error_jsp.java:48: not a statement ex; ^ 1 error 异常: class org.apache.jasper.JasperException 这样是怎么做出来的呀????????????????急
给楼主一段书上的例子,建议多看看基础知识。下面的程序试图抛出一个它不捕捉的异常,因为这个程序没有指定一个throws子句来声明抛出异常,所以程序不能编译通过。 【例6.4】无声明的抛出异常 public class Exception6 { static void throwOne() { System.out.println ("抛出内部异常。"); throw new IllegalAccessException ("demo"); } public static void main (String[] args) { throwOne(); } } 为了使这个程序编译成功,需要进行两处修改。首先,需要声明方法throwOne()抛出了IllegalAccessException异常。然后,必须在main()中定义try-catch语句来捕捉这个异常。修改的正确程序如下: 【例6.5】声明抛出异常 public class Exception7 { static void throwOne() throws IllegalAccessException { System.out.println ("抛出内部异常。"); throw new IllegalAccessException ("demo"); } public static void main (String[] args) { try{ throwOne(); } catch(IllegalAccessException e){ System.out.println ("捕捉到异常为:"+e); } } } 运行结果如下: 抛出内部异常。 捕捉到异常为:java.lang.IllegalAccessException: demo
public void initData(String dataDriver) throws ClassNotFoundException{
Class.forName(dataDriver);
}
没有try{..}catch(..){}这样的语句还有:有了 throws Exception 还能用TRY{}CATCH(){}语句吗??????
......
}===>>public String name_fun(){
try{
......
}catch(Exception ex){
...;//异常处理
}
}
try{
//String dataDriver
String returnValue = name_fun(dataDriver)
}catche(ClassNotFoundException ex){
ex.printStackTrace();
//你自己的处理
}
public String name_fun(String dataDriver ){
try{
Class.forName(dataDriver);
}catch(ClassNotFoundException ex){
...;//异常处理
throw ex;
}
}
我在index.jsp中定义了:<%@ page isErrorPage="true" errorPage="error.jsp"%>
并且error.jsp这样写:
<%@ page isErrorPage="true" language="java" contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*" %>
<%@ page errorPage="error.jsp" %>
<%
Exception ex;
ex;
if(ex.getMessage()!=null) out.print(ex.getMessage());
%>
结果报错
错误码: 500
讯息: Unable to compile class for JSP An error occurred at line: 3 in the jsp file: /sam/test/manage/error.jsp Generated servlet error: [javac] Compiling 1 source file D:\Sun\AppServer\domains\jakarta-tomcat-4.1.30\work\Standalone\localhost\_\sam\test\manage\error_jsp.java:48: not a statement ex; ^ 1 error
异常: class org.apache.jasper.JasperException
这样是怎么做出来的呀????????????????急
【例6.4】无声明的抛出异常
public class Exception6
{
static void throwOne()
{
System.out.println ("抛出内部异常。");
throw new IllegalAccessException ("demo");
}
public static void main (String[] args)
{
throwOne();
}
}
为了使这个程序编译成功,需要进行两处修改。首先,需要声明方法throwOne()抛出了IllegalAccessException异常。然后,必须在main()中定义try-catch语句来捕捉这个异常。修改的正确程序如下:
【例6.5】声明抛出异常
public class Exception7
{
static void throwOne() throws IllegalAccessException
{
System.out.println ("抛出内部异常。");
throw new IllegalAccessException ("demo");
}
public static void main (String[] args)
{
try{
throwOne();
}
catch(IllegalAccessException e){
System.out.println ("捕捉到异常为:"+e);
}
}
}
运行结果如下:
抛出内部异常。
捕捉到异常为:java.lang.IllegalAccessException: demo