tag:blogger.com,1999:blog-28606456381048755272024-03-09T06:54:16.325+05:30Chirag's Computer BlogA dose of my technical experience...chirag jainhttp://www.blogger.com/profile/12266084272384833275noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-2860645638104875527.post-85875748235169285992009-11-28T10:13:00.002+05:302009-11-28T10:20:41.723+05:30Java Exceptions: Using Exception's Sub Classes<span style="font-weight:bold;">Syntax of try-catch</span><br /><br /><span style="font-style:italic;">try<br />{<br /><br />//code which throws some exception<br />}<br /><br />catch(XException xe)<br />{<br /><br /><br />}</span> where X is type of exception.<br /><br />In catch, we have to pass type of exception which is thrown by the code in above try <br />block.If we know the exact type of excepton thrown, we should use it in catch, as:<br /><br /><span style="font-style:italic;">catch(SQLException se)<br />{<br /><br /><br />}</span><br /><br />But what if we are not sure about type of exception thrown? Here is the solution:<br /><br /><br /><br /><span style="font-style:italic;">catch(Exception e)<br />{<br /><br /><br />}</span><br /><br />Exception is superclass of all Exception classes. So a reference of Exception can catch all type of exceptions.<br />So whatever is the exception type, it will catch it and will not give any error.<br />To know the type of exception, we can print it:<br /><br /><span style="font-style:italic;">catch(XException e)<br />{<br />System.out.println("Exception type: "+e);<br /><br />}<br /><br /><br /></span>Caution: If we are using multiple catch blocks for a single try, always <span style="font-weight:bold;">put superclass before subclass</span>, as:<br /><br /><span style="font-style:italic;">try<br />{<br /><br />// code here<br />}<br /><br />catch(XException xe)<br />{<br /><br /><br />}<br /><br />catch(Exception e)<br />{<br /><br /><br />}<br /><br /></span>where XException may be any exception as SQLException,ClassNotFoundException etc. <br /><br />Putting Exception before XException will produce error.<br /><br /><span style="font-style:italic;"><br />try<br />{<br /><br />// code here<br />}<br /><br />/* this will give compilation error<br />catch(Exception e)<br />{<br /><br /><br />}<br /><br />catch(XException xe)<br />{<br /><br /><br />}<br /><br />*/<br /></span>chirag jainhttp://www.blogger.com/profile/12266084272384833275noreply@blogger.comtag:blogger.com,1999:blog-2860645638104875527.post-24913919347634722862009-11-19T22:12:00.001+05:302009-11-19T22:14:59.943+05:30Java Basics: How to set PATH and CLASSPATHWhen we write a java program and put the source file(.java file) in a location other than<br />bin of jdk, java compiler can not find the necessary class files of java api(as java.lang.*,<br />for example) to compile our file.So we need to set PATH and CLASSPATH environment variables<br />in our system. Here are the steps:<br /><br />Adding Path:<br />1. Go to my computer<br />2.Right click, go to properties.<br />3. Go to Advanced tab.<br />4. Go to Environment Variables tab.<br />5. Search for PATH, then click on edit.<br />6. Add the path of your JDK bin folder, as C:\Program Files\Java\jdk1.5.0_15\bin<br />7. Click OK.<br /><br />Adding Classpath:<br /><br />1. Go to my computer<br />2.Right click, go to properties.<br />3. Go to Advanced tab.<br />4. Go to Environment Variables tab.<br />5. Search for CLASSPATH, then click on edit.<br />6. If it is not there, then add it by clicking NEW.<br />7. Add the path of your JDK lib folder, as C:\Program Files\Java\jdk1.5.0_15\lib<br />7. Click OK.<br /><br />By these steps,we can execute javac command under any directory in our system.<br />After these steps, open a new cmd prompt window, and compile your program, as:<br />C:\Documents and Settings\chirag.jain\Desktop>javac Program.java<br /><br />After compilation, run the program as:<br /><br />C:\Documents and Settings\chirag.jain\Desktop>java Programchirag jainhttp://www.blogger.com/profile/12266084272384833275noreply@blogger.com0tag:blogger.com,1999:blog-2860645638104875527.post-62474916297139585472009-11-19T21:57:00.002+05:302009-11-19T22:19:51.958+05:30Getting the source code from java .class fileWhen we compile .java file using <code> javac</code>, we get .class file. But we can get the source code from class file, but not the full source code. <code> javap</code>, is the utility which gives details of variables and methods declared in the code.<br /><br /><code><br />C:\Documents and Settings\chirag.jain\Desktop>javac Test.java<br /><br />C:\Documents and Settings\chirag.jain\Desktop>javap Test<br /><br />Compiled from “Test.java”<br />public class Test extends java.lang.Object{<br />int number;<br />public Test();<br />public void sayHello();<br />}<br /></code><br /><br />To get the byte code details, use <code> javap -c </code><br /><br /><code><br />C:\Documents and Settings\chirag.jain\Desktop>javap -c Test<br /><br />Compiled from “Test.java”<br />public class Test extends java.lang.Object{<br />int number;<br />public Test();<br />Code:<br />0: aload_0<br />1: invokespecial #1; //Method java/lang/Object.”<init>”:()V<br />4: aload_0<br />5: iconst_5<br />6: putfield #2; //Field number:I<br />9: return<br />public void sayHello();<br />Code:<br />0: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;<br />3: ldc #4; //String Hello<br />5: invokevirtual #5; //Method java/io/PrintStream.println:(Ljava/lang/Str<br />ing;)V<br />8: return<br /><br />}<br /><br /></code>chirag jainhttp://www.blogger.com/profile/12266084272384833275noreply@blogger.com0