Results 1 to 8 of 8
Thread: throws - Question
- 08-21-2014, 12:31 PM #1
Member
- Join Date
- Aug 2014
- Posts
- 4
- Rep Power
- 0
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(); }
Java Code:public static void main(String[] args) throws IOException
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 12:33 PM.
- 08-21-2014, 12:45 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-21-2014, 01:09 PM #3
Member
- Join Date
- Aug 2014
- Posts
- 4
- Rep Power
- 0
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)
- 08-21-2014, 01:29 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: throws - Question
Exactly. So. Is that what you want to happen?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-21-2014, 02:16 PM #5
Member
- Join Date
- Aug 2014
- Posts
- 4
- Rep Power
- 0
Re: throws - Question
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); } } }
We useJava Code:throws
Java Code:static void throwOne() throws IllegalAccessException
Java Code:public static void main(String args[])
Java Code:try-catch
So I just want to understand how does java runtime handles exceptionJava Code:public static void main(String args[]) throws IOException
Does java use standard runtime error handler or it's much more complicated?Last edited by Zhenya; 08-21-2014 at 02:23 PM.
- 08-21-2014, 03:25 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-21-2014, 03:51 PM #7
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: throws - Question
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-21-2014, 05:28 PM #8
Member
- Join Date
- Aug 2014
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
throws
By java4amanda in forum New To JavaReplies: 2Last Post: 03-29-2012, 10:08 AM -
Throws and Throw
By f22raptor in forum New To JavaReplies: 2Last Post: 09-04-2011, 09:28 AM -
throws exception
By simorgh in forum New To JavaReplies: 1Last Post: 07-30-2010, 01:24 AM -
Execute() throws an NPE
By mjz in forum JDBCReplies: 0Last Post: 08-06-2009, 03:25 AM -
throws
By jdgallag in forum New To JavaReplies: 14Last Post: 02-11-2009, 02:07 AM
Bookmarks