Results 1 to 14 of 14
- 05-01-2010, 11:54 AM #1
Member
- Join Date
- May 2010
- Location
- France, Paris
- Posts
- 9
- Rep Power
- 0
What's wrong with my (simple) code
I have this message when it si compiledJava Code:import java.net.InetAddress; public class Test2 { public static void main (String[] args) { InetAddress localaddr = InetAddress.getLocalHost(); System.out.println ("Local IP Address : " + localaddr); System.out.println ("Local hostname : " + localaddr.getHostName()); } }
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problem: at first.Test2.main(Test.java:13)
- 05-01-2010, 12:20 PM #2
Member
- Join Date
- May 2010
- Location
- France, Paris
- Posts
- 9
- Rep Power
- 0
The line 13 is actually the line"public static void main (String[] args) {".
I didn't paste the whole code.
Thanks
-
I don't see you handling an exception:
What happens if you do this?Java Code:import java.net.InetAddress; import java.net.UnknownHostException; public class Test2 { public static void main(String[] args) throws UnknownHostException { InetAddress localaddr = InetAddress.getLocalHost(); System.out.println("Local IP Address : " + localaddr); System.out.println("Local hostname : " + localaddr.getHostName()); } }
-
- 05-01-2010, 01:00 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
What surprises me is that the compiler generated a class file despite the fact that a (checked) UnknownHostException wasn't trapped or declared to be thrown. I bet it at least warned about that fact (and should've aborted the entire compilation task without generating a .class file).
kind regards,
Jos
-
- 05-01-2010, 01:27 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-01-2010, 03:42 PM #8
Member
- Join Date
- May 2010
- Location
- France, Paris
- Posts
- 9
- Rep Power
- 0
Here is the whole code.
[CODE]
package myTestingPackage;
import java.net.InetAddress;
public class Test2 {
public static void main (String[] args) {
InetAddress localaddr = InetAddress.getLocalHost();
System.out.println ("Local IP Address : " + localaddr);
System.out.println ("Local hostname : " + localaddr.getHostName());
}
}
[\CODE]
And here uis the error message. It has changed but I don't know why!
Java Code:Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type UnknownHostException at myTestingPackage.Test2.main(Test2.java:7)
- 05-01-2010, 03:46 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 05-01-2010, 03:47 PM #10
Member
- Join Date
- May 2010
- Location
- France, Paris
- Posts
- 9
- Rep Power
- 0
- 05-01-2010, 03:49 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Checked exceptions have to be handled by your code; either by declaring that your code lets it pass "throws ACheckedException" or by actually catching the exception and handling it. The compiler checks this and refuses to compile any code that doesn't handle such exceptions. It's a safety net.
kind regards,
Jos
- 05-01-2010, 03:51 PM #12
Member
- Join Date
- May 2010
- Location
- France, Paris
- Posts
- 9
- Rep Power
- 0
- 05-01-2010, 03:53 PM #13
Member
- Join Date
- May 2010
- Location
- France, Paris
- Posts
- 9
- Rep Power
- 0
- 05-01-2010, 03:55 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
If a method states that it can throw an Exception it isn't obliged to always throw that Exception. It only throws it in exceptional circumstances. This method only throws that Exception if it can't find a local host and your code is supposed to handle that Exception when/if it is thrown because it is a 'checked' Exception. Your compiler checks it (as you already have noticed).
kind regards,
Jos
Similar Threads
-
Need help! something wrong in my code
By novak100 in forum New To JavaReplies: 2Last Post: 11-18-2009, 11:59 PM -
Simple search-function: what am I doing wrong?
By Bas in forum New To JavaReplies: 4Last Post: 07-23-2009, 09:45 PM -
pls tell wat wrong with my code???
By low224 in forum New To JavaReplies: 13Last Post: 01-11-2009, 07:40 AM -
Simple Addition Program Outputting Wrong Value
By carlodelmundo in forum New To JavaReplies: 4Last Post: 08-05-2008, 03:37 AM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks