Results 1 to 8 of 8
  1. #1
    Zhenya is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Question throws - Question

    Good afternoon for everyone!
    I have a question:
    Java Code:
    public static void main(String[] args) throws IOException{
    			double a,b,c;
    				Scanner input = new Scanner(System.in);
    				System.out.println("Pass a parameter");
    				a = input.nextDouble();
    				if(a==0){
    					throw new IOException("a parameter can't be equal to 0");
    				}
    				System.out.println("pass b parameter");
    				b = input.nextDouble();
    				System.out.println("pass c parameter");
    				c = input.nextDouble();
    				SquareEq Equation = new SquareEq(a, b, c);
    		        if(b==0||c==0){
    		        	Equation.halfCount();
    		        }
    		        else
    		        	Equation.fullCount();
            
    	}
    Is it right to do like this:
    Java Code:
    public static void main(String[] args) throws IOException
    I've already read about throws and it's anounced that throws is used in methods which can't process the exception, but it must be processed in outter try-catch block in which the method with declared throws is executed!
    But main is executed by Java runtime!
    So this exception will not be processed and we can't write like this?? Am I right??
    Last edited by Zhenya; 08-21-2014 at 11:33 AM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: throws - Question

    You can wonder about it, or you can test it out. What happens when you trip an IOException, which in your code is as simple as inputting 0?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Zhenya is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default Re: throws - Question

    I've already tested it!
    when i pass 0 it throws exception:
    Exception in thread "main" java.io.IOException: a parameter can't be equal to 0
    at newPackage.root.main(root.java:51)

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: throws - Question

    Exactly. So. Is that what you want to happen?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Zhenya is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default Re: throws - Question

    Quote Originally Posted by gimbal2 View Post
    Exactly. So. Is that what you want to happen?
    No, I need explanation!
    Here is example from Herbert Schildt
    Java Code:
    class ThrowsDemo {
      static void throwOne() throws IllegalAccessException {
        System.out.println("Inside throwOne.");
        throw new IllegalAccessException("demo");
      }
      public static void main(String args[]) {
        try {
          throwOne();
        } catch (IllegalAccessException e) {
          System.out.println("Caught " + e);
        }
      }
    }
    This example is simple and I understand it!
    We use
    Java Code:
    throws
    here
    Java Code:
    static void throwOne() throws IllegalAccessException
    because we don't want to handle exception here, we handle it in
    Java Code:
    public static void main(String args[])
    , in which we call this method and it wrapped in
    Java Code:
    try-catch
    block!
    So I just want to understand how does java runtime handles exception
    Java Code:
     public static void main(String args[]) throws IOException
    This method is executed by java runtime!!!
    Does java use standard runtime error handler or it's much more complicated?
    Last edited by Zhenya; 08-21-2014 at 01:23 PM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: throws - Question

    When a method 'throws X', it tells the compiler that it might throw an X (but doesn't have to). If your main( ... ) method throws X, and it does so, the JVM (the caller of the main( ... ) method) has to handle that X. The JVM can't and produces a stack trace and terminates.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: throws - Question

    Quote Originally Posted by Zhenya View Post
    Does java use standard runtime error handler or it's much more complicated?
    What is so complicated about it? It prints the error and quits - that is the behavior you yourself already observed and it cannot be any more simple. What kind of mysteries are you trying to find that aren't there? I have no clue.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    Zhenya is offline Member
    Join Date
    Aug 2014
    Posts
    4
    Rep Power
    0

    Default Re: throws - Question

    Quote Originally Posted by JosAH View Post
    When a method 'throws X', it tells the compiler that it might throw an X (but doesn't have to). If your main( ... ) method throws X, and it does so, the JVM (the caller of the main( ... ) method) has to handle that X. The JVM can't and produces a stack trace and terminates.

    kind regards,

    Jos
    That's what i need!
    Thank you for help!

Similar Threads

  1. throws
    By java4amanda in forum New To Java
    Replies: 2
    Last Post: 03-29-2012, 09:08 AM
  2. Throws and Throw
    By f22raptor in forum New To Java
    Replies: 2
    Last Post: 09-04-2011, 08:28 AM
  3. throws exception
    By simorgh in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 12:24 AM
  4. Execute() throws an NPE
    By mjz in forum JDBC
    Replies: 0
    Last Post: 08-06-2009, 02:25 AM
  5. throws
    By jdgallag in forum New To Java
    Replies: 14
    Last Post: 02-11-2009, 01:07 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
  •