Page 1 of 2 12 LastLast
Results 1 to 20 of 33
  1. #1
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default Doubt in Exception

    Hello All,

    I have one doubt in Exception in Java.

    Can we catch every Exception and Error in our program using try catch block?


    please give some thought.


    sanjeev,संजीव

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ya, I think so. If we write our own Exceptions correctly, it's ok. Did you try to do it in any other way?

  3. #3
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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
    Java Code:
    public class CheckException1 
    {
    	public static void main(String[] args){
    		System.out.println("in CheckException...");
    	}
    }
    TestCheckException.java
    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,संजीव

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, I'll try it. But one thing. Why did you execute the CheckException.java, rather CheckException1.java?

  5. #5
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    Ok, I'll try it. But one thing. Why did you execute the CheckException.java, rather CheckException1.java?
    Just to get the exception and to check this....R&D only.
    sanjeev,संजीव

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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 01:16 PM. Reason: adding more

  7. #7
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Where did you get this ..... in TestCheckException.java file....

    but i am not getting this.... program running fine

    sanjeev,संजीव

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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....???

  9. #9
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    But i am not getting dear
    can please detail some more about this
    sanjeev,संजीव

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    For the min change the process execution line for other file which you don't have in the working package. Maybe like this,

    Java Code:
    Process proc = Runtime.getRuntime().exec("javac sanjeev.java");
    I assume that, there is no such a file. ;) What error message you got?

  11. #11
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default



    I am not getting any error message............
    sanjeev,संजीव

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    I can't be happened my friend. Just run the TestCheckException class. You should get the error.

  13. #13
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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,संजीव

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  15. #15
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Quote Originally Posted by Eranga View Post
    It wont be pal. Try it with IOException. Which IDE you use.
    I used Exception class then .....there is no need for IOException
    well i also used IOException but nothing changed.
    Using Notepad and compiling using Console(Command Prompt on Windows).
    sanjeev,संजीव

  16. #16
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Have you check the javac classpath eranga? (just to confirm)...
    freedom exists in the world of ideas

  17. #17
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    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

  18. #18
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    If you just want to catch all exceptions just

    Java Code:
    try
    {
       what ever you want to test
    
    }
    catch(Exception e)
    { 
        e.printStackTrace();
    }

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by sukatoa View Post
    Have you check the javac classpath eranga? (just to confirm)...
    Oops, I forget it. Just work on Netbeans and forget to do it. Thanks for pointing me.

  20. #20
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Zosden View Post
    If you just want to catch all exceptions just

    Java Code:
    try
    {
       what ever you want to test
    
    }
    catch(Exception e)
    { 
        e.printStackTrace();
    }
    Yes, but sanjeev work on his own exception class there. Stack trace always give a quite well explained exceptions.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 03-08-2008, 07:41 PM
  2. doubt
    By shaju_inspira in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-18-2008, 09:38 AM
  3. doubt in jms
    By veena in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 01-05-2008, 02:42 PM
  4. Doubt on <bean> tag
    By sudhakarbe in forum Web Frameworks
    Replies: 0
    Last Post: 11-22-2007, 08:57 AM
  5. Doubt with classpath
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 02:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •