Tuesday 17 July 2012

Multiple Catch statements in JAVA




Multiple catch Clauses
In some cases, more than one exception could be raised by a single piece of code. To handle
this type of situation, you can specify two or more catch clauses, each catching a different
type of exception. When an exception is thrown, each catch statement is inspected in order,
and the first one whose type matches that of the exception is executed. After one catch
statement executes, the others are bypassed, and execution continues after the try/catch
block. The following example traps two different exception types:
// Demonstrate multiple catch statements.
class MultiCatch {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
int c[] = { 1 };
c[42] = 99;
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks.");
}
}
This program will cause a division-by-zero exception if it is started with no command-
line arguments, since a will equal zero. It will survive the division if you provide a
command-line argument, setting a to something larger than zero. But it will cause an
ArrayIndexOutOfBoundsException, since the int array c has a length of 1, yet the program
attempts to assign a value to c[42].
Here is the output generated by running it both ways:
C:\>java MultiCatch
a = 0
Divide by 0: java.lang.ArithmeticException: / by zero
After try/catch blocks.
C:\>java MultiCatch TestArg
a = 1
Array index oob: java.lang.ArrayIndexOutOfBoundsException:42
After try/catch blocks.
When you use multiple catch statements, it is important to remember that exception
subclasses must come before any of their superclasses. This is because a catch statement
that uses a superclass will catch exceptions of that type plus any of its subclasses. Thus, a
subclass would never be reached if it came after its superclass. Further, in Java, unreachable
code is an error. For example, consider the following program:
/* This program contains an error.
A subclass must come before its superclass in
a series of catch statements. If not,
unreachable code will be created and a
compile-time error will result.
*/
class SuperSubCatch {
public static void main(String args[]) {
try {
int a = 0;
int b = 42 / a;
} catch(Exception e) {
System.out.println("Generic Exception catch.");
}
/* This catch is never reached because
ArithmeticException is a subclass of Exception. */
catch(ArithmeticException e) { // ERROR - unreachable
System.out.println("This is never reached.");
}
}
}
If you try to compile this program, you will receive an error message stating that the
second catch statement is unreachable because the exception has already been caught. Since
ArithmeticException is a subclass of Exception, the first catch statement will handle all
Exception-based errors, including ArithmeticException. This means that the second catch
statement will never execute. To fix the problem, reverse the order of the catch statements.


Note : the above simple (yet appears to be recondite ) information was taken from Java - The complete reference  book.

Tuesday 3 July 2012

Rubbish

Dont read this ....
How serving webpages works ? (according to me )

we click url  and page correspondingly is served from server
we then give input and click submit
then this info is sent in request
then now 2 things can be done
1) either values are written directly in response by response.write () in the java program(servlet)
2) or use JSP ,where it is done in two ways
i) values are set in bean(bean should exists) in server and then got from bean by

<jsp:useBean id="mybean" scope="session" class="org.mypackage.hello.NameHandler" />
 <jsp:setProperty name="mybean" property="name" value="vivek" /> (sets value of the argument in url "name" and sets in bean )
 <jsp:getProperty name="mybean" property="name1"/> ( retrieves from bean)
ii) or write java code in scriptlet tag
when you create web app using struts additional files are ?
Name->Web Pages->WEB-INF ->
1) beans.xml
2) struts-config.xml
3)tiles-defs.xml
4)validation.xml
5) validator-rules.xml


Actually what we do in including struts is
actually in giving global url localhost:8080/global-url/ , it gets directed to somename.do ,,, as in index.jsp we have asked it to forward the request <jsp:forward page="somename.do"/> , this statement is equal to typing in url tab as localhost:8080/global-url/somename.do  . Dont interpret this statement as forwaring request to somename.do (as it looks smilar to redirecting to some.jsp) .. but this is eq to typing and clicking  url/somename.do .
now it goes and checks in web.xml . what to do with request localhost:8080/global-url/somename.do  
as there it is written as *.do as context path should be mapped to servlet org.apache.struts.action.ActionServlet , and also struts-config.xml are sent as parameters .........


so now thereafter it becomes regular servlet story , as  org.apache.struts.action.ActionServlet class has a reference to what i am saying. statement goes like this
 public class ActionServlet extends javax.servlet.http.HttpServlet { 
so  org.apache.struts.action.ActionServlet is our servlet java code 


and the struts-config.xml is like the original web.xml 


The idea of having this type of mapping is that they have servlet classes org.apache.struts.action.* written by themselves which does some of the basic tasks . 


As it completely deals with srtuts action , people have created their own nomenclature . like using 
<action-mapping> instead of <servlet-mapping>