Showing posts with label Exception Handling. Show all posts
Showing posts with label Exception Handling. Show all posts

July 14, 2011

Declarative Exception Handling in Struts 2

1) Create an action class - ErrorProne.java - and place it in the src folder of the Web Application. The execute() method of this class throws an exception,

package info.icontraining.struts2;

public class ErrorProne { 
   public String execute() throws Exception {
      throw new Exception ( "Routine Code Explosion");
   }
}

2) Configure the action in the struts.xml file within the package element

<package ... >
   ...
   <global-results>
      <result name="Error">/error-struts2.jsp</result>
   </global-results>

   <global-exception-mappings>
      <exception-mapping exception="java.lang.Exception" result="Error"/>
   </global-exception-mappings>
   ...
   <action name="ErrorProne" class="info.icontraining.struts2.ErrorProne">
   ...
</package>

3) Create the custom JSP that will be displayed when the exception occurs - error-struts2.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
  <p>This is a custom error page that displays when an Exception is thrown in any Struts2 component</p>
  <br/>
  <s:property value="%{exception.message}" />
  <s:property value="%{exceptionStack}" />
</body>
</html>

3) Test the code with the following URL in the browser,

http://localhost:8080/WebAppName/ErrorProne.action

April 12, 2011

Exception Handling in a JSP


Write a JSP - test.jsp. Within a scriptlet in this JSP, write code that throws an Exception (for example: divide by zero Arithmetic Exception). Write another JSP - error.jsp. Display a message in this JSP that says "An unexpected error occured"
Make the error.jsp display when the client requests test.jsp

Solution


1) Create a JSP - test.jsp - in the WebContent folder with the following code that throws an ArithmeticException

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Test Page</title>
</head>
<body>

<% int i = 3/0; %>
<%-- This line will throw an ArithmeticException --%>

</body>
</html>

2) Configure exception-handling for ArithmeticException in the WebContent/WEB-INF/web.xml file

<error-page>
   <exception-type>java.lang.ArithmeticException</exception-type>
   <location>/error.jsp</location>
</error-page>

3) Create an error.jsp JSP in the WebContent folder with the isErrorPage attribute of the page directive set to true. This JSP has access to the exception JSP implicit object since it is now a designated error page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage="true" %>
<html>
<head>
<title>Custom Error Page</title>
</head>
<body>

<p>This is a custom error page that displays when an ArithmeticException is thrown in any JSP</p>

<br/>
${pageContext.exception.message}

</body>
</html>

4) Test the code by typing the following URL in the browser

http://localhost:8080/WebAppName/test.jsp

Note: If the EL expression ${ ... } does not print anything, set the isELIgnored attribute of the page directive to false as follows,

<%@ page ... isELIgnored="false" %>

February 5, 2011

Exception Handling in Java

Java's Exception-handling mechanism cleanly separates the exception-generating code from the exception-handling code.


import java.io.*;
import java.sql.*;

public class Test {

   public void m1() throws ClassNotFoundException {

      try {
  
         FileReader f = new FileReader("test.txt");
                   // throws FileNotFoundException

         Connection c = DriverManager.getConnection("test");
                   // throws SQLException

         Class.forName(info.icontraining.Test);
                   // throws ClassNotFoundException

      } catch (FileNotFoundException e) {
         System.err.println(e.getMessage());
      } catch (SQLException e) {
         System.err.println(e.getMessage()); 
      } catch (Exception e) {
         System.err.println("catch all other exceptions");
         System.err.println(e.getMessage());
      } finally {
         System.out.println("In the finally block");
      }
   }

   public static void main(String[] args) {

      Test t = new Test();
      
      try {
         t.m1();
      } catch (ClassNotFoundException e) {
         System.err.println(e.getMessage());
      }
   }
}