Results 1 to 20 of 33
Thread: Doubt in Exception
- 04-24-2008, 11:30 AM #1
- 04-24-2008, 11:33 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ya, I think so. If we write our own Exceptions correctly, it's ok. Did you try to do it in any other way?
- 04-24-2008, 11:47 AM #3
Yes pal ,
try this example.
I have one java file named CheckException.java(very simple) It's compiling fine.
I have saved this file with the same name as class name as usual but when i change class name to CheckException1 and try to compile it gives error on command prompt :CheckException.java:1: class CheckException1 is public, should be declared in a file named CheckException1.java ..... as usual it will give, this is OK.
Now i created another class Named TestCheckException.java in which using Runtime i am compiling CheckException.java by changing the class name to CheckException1(in CheckException.java), and putting code in try catch......but i am not able to catch the Exception or Error.
Try to run these example.
CheckException.java
TestCheckException.javaJava Code:public class CheckException1 { public static void main(String[] args){ System.out.println("in CheckException..."); } }
Java Code:import java.io.*; public class TestCheckException { public static void main(String[] args){ try{ Process proc = Runtime.getRuntime().exec("javac CheckException.java"); }catch(Exception err){ // or use catch(Throwable err) System.out.println("ERROR...."+err); } } }sanjeev,संजीव
- 04-24-2008, 12:09 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok, I'll try it. But one thing. Why did you execute the CheckException.java, rather CheckException1.java?
- 04-24-2008, 12:12 PM #5
- 04-24-2008, 12:14 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you execute a command line argument using Process in Java? :) For me I got the exception as "ERROR....Cannot run program "javac": CreateProcess error=2, The system cannot find the file specified"
So...Last edited by Eranga; 04-24-2008 at 12:16 PM. Reason: adding more
- 04-24-2008, 12:20 PM #7
Where did you get this ..... in TestCheckException.java file....
but i am not getting this.... program running fine
sanjeev,संजीव
- 04-24-2008, 12:26 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
From your code pal. That's ok, isn't it. Exception point that javac can't run as a process. That's why I'm asking it....???
- 04-24-2008, 12:29 PM #9
But i am not getting dear
can please detail some more about this
sanjeev,संजीव
- 04-24-2008, 12:39 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
For the min change the process execution line for other file which you don't have in the working package. Maybe like this,
I assume that, there is no such a file. ;) What error message you got?Java Code:Process proc = Runtime.getRuntime().exec("javac sanjeev.java");
- 04-24-2008, 12:43 PM #11
I am not getting any error message............
sanjeev,संजीव
- 04-24-2008, 12:45 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I can't be happened my friend. Just run the TestCheckException class. You should get the error.
- 04-24-2008, 12:48 PM #13
I am running this file as you told me.
but i am not getting any error message on console
Java Code:import java.io.*; public class TestCheckException { public static void main(String[] args){ try{ Process proc = Runtime.getRuntime().exec("javac sanjeev.java"); }catch(Exception err){ // or use catch(Throwable err) System.out.println("ERROR...."+err); } } }sanjeev,संजीव
- 04-24-2008, 12:52 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It wont be pal. Try it with IOException. Which IDE you use.
- 04-24-2008, 12:57 PM #15
- 04-24-2008, 02:09 PM #16
Have you check the javac classpath eranga? (just to confirm)...
freedom exists in the world of ideas
- 04-24-2008, 05:21 PM #17
Eranga, you get this error (ERROR....java.io.IOException: Cannot run program "javac": CreateProcess error=2, The system cannot find the file specified) because the javac executable is not in the system path. If you specify the full path you will get no exception because the code finds javac and executes it.
Sanjeev - not getting an exception is normal because none is thrown. You are executing another process and even if that process gets errors you don't check for them. You have to manage the started process and know its interfaces to get the errors. On the other hand if you would run the Java compiler programmatically, then you would get an exception because your target Java file is not there.Daniel @ [www.littletutorials.com]
Language is froth on the surface of thought
- 04-24-2008, 06:21 PM #18
If you just want to catch all exceptions just
Java Code:try { what ever you want to test } catch(Exception e) { e.printStackTrace(); }
- 04-25-2008, 04:28 AM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 04-25-2008, 04:31 AM #20
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Similar Threads
-
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM -
doubt
By shaju_inspira in forum Sun Java Wireless ToolkitReplies: 0Last Post: 01-18-2008, 08:38 AM -
doubt in jms
By veena in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 01-05-2008, 01:42 PM -
Doubt on <bean> tag
By sudhakarbe in forum Web FrameworksReplies: 0Last Post: 11-22-2007, 07:57 AM -
Doubt with classpath
By fred in forum New To JavaReplies: 1Last Post: 07-24-2007, 01:58 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks