Results 1 to 6 of 6
  1. #1
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default when is throw used in try block

    In Exception Handling,

    I read that "throw" is used to throw the exception object in try block for user defined exceptions.

    As far as I know, if the exception occurs in try block, it would be handled by catch block.

    For Ex:
    class Test{
    public static void main(String args[]){
    try{
    System.out.println(args[0]/args[1]);
    }
    catch(Exception e){
    System.out.println("Error Occured");
    }

    }
    }

    In the above pgrm, throw clause is not used, so is it that throw is only used for user defined exceptions.
    Can someone please explain..

    Thanks.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: when is throw used in try block

    It depends upon the purpose of the method. If there was some code in the method that you always wanted to run then you could include it in a finally clause regardless if an excpetion was thrown or not. Consider the following example:
    Java Code:
    public class Test {
        public void run() {
            method1();
            method2();
        }
    
        private void method1() {
            try {
                doStuff1(-1);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
        
        private void doStuff1(int value) throws Exception {
            try {
                if(value < 0) {
                    throw new Exception("Danger Will Robinson!");
                }
            } catch(Exception e) {
                throw e;
            } finally {
                System.out.println("Done!");
            }
        }
    
        private void method2() {
            try {
                doStuff2(-1);
            } catch(Exception e) {
                e.printStackTrace();
            }
        }
    
        private void doStuff2(int value) throws Exception {
            if(value < 0) {
                throw new Exception("Danger Will Robinson!");
            }
            System.out.println("Done!");
        }
    
    
        public static void main(String[] args) throws Exception {
            new Test().run();
        }
    }
    In the method1/doStudff1 version "Done" is displayed but in method2/doStuff2 it is not because the exception being thrown circumvents it happening.

  3. #3
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: when is throw used in try block

    So its not necessary that 'throw' should be used only for user defined exceptions, it can be used anywhere depending on the situations or conditions of the program. 'throw' can be used within try block, but its not compulsory. and throw can be used in a method that throws an Exception (a method declared with throws clause) but not compulsory (it can be handled using try catch block without throw).

    Please correct me if Iam wrong.

    Thanks.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: when is throw used in try block

    Quote Originally Posted by cooladithi4u@yahoo.com View Post
    So its not necessary that 'throw' should be used only for user defined exceptions
    That's right: you can throw an exception of any kind once you recognise that an exception has happened but you don't want to deal with it (or can't deal with it). You throw the exception and let the caller deal with it.

    As Junky's example shows you can throw the exception but also have a "finally" section that will do any required clean up or other work.

  5. #5
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default Re: when is throw used in try block

    Only thing Im unsure with the example is that:
    why are we declaring main method with 'throws Exception', why not the run() because run() is calling the methods, method1 and method2 which actually may throw exceptions.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: when is throw used in try block

    Quote Originally Posted by cooladithi4u@yahoo.com View Post
    Only thing Im unsure with the example is that:
    why are we declaring main method with 'throws Exception',
    That was left over from a different example. Just ignore it.

Similar Threads

  1. Throws and Throw
    By f22raptor in forum New To Java
    Replies: 2
    Last Post: 09-04-2011, 09:28 AM
  2. where to use throw and throws
    By javastuden in forum New To Java
    Replies: 4
    Last Post: 11-02-2010, 04:31 PM
  3. About throw message
    By killerf2006 in forum New To Java
    Replies: 4
    Last Post: 08-22-2010, 12:48 PM
  4. what exception to throw
    By DoolinDalton in forum New To Java
    Replies: 5
    Last Post: 02-10-2010, 04:45 PM
  5. throw an exception
    By sfe23 in forum New To Java
    Replies: 3
    Last Post: 02-14-2009, 05:41 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
  •