Java – Create JSP Error Page to Handle Exceptions

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Error Page</title>
</head>
<body>
<h1>An error has occurred.</h1>

<div style="color: #F00;">
Error message: <%= exception.toString() %>
</div>
</body>
</html>

Java – Handle EmptyStackException

import java.util.Date;
import java.util.EmptyStackException;
import java.util.Stack;

public class ExceptionalTest {
   public static void main(String[] args) {
      int count = 1000000;
      Stack s = new Stack();
      System.out.println("Testing for empty stack");
      long s1 = System.currentTimeMillis();
      for (int i = 0; i <= count; i++)
      if (!s.empty())
         s.pop();
      long s2 = System.currentTimeMillis();
      System.out.println((s2 - s1) + " milliseconds");
      System.out.println("Catching EmptyStackException");
      s1 = System.currentTimeMillis();
      for (int i = 0; i <= count; i++) {
         try {
            s.pop();
         }
         catch (EmptyStackException e) {
         }
      }
      s2 = System.currentTimeMillis();
      System.out.println((s2 - s1) + " milliseconds");
   }
}

Java – Create User Defined Exception

class WrongInputException extends Exception {
   WrongInputException(String s) {
      super(s);
   }
}
class Input {
   void method() throws WrongInputException {
      throw new WrongInputException("Wrong input");
   }
}
class TestInput {
   public static void main(String[] args){
      try {
         new Input().method();
      }
	  catch(WrongInputException wie) {
         System.out.println(wie.getMessage());
      }
   }
}