Results 1 to 9 of 9
Thread: What to do here(cant compile)?
- 08-04-2011, 04:33 PM #1
What to do here(cant compile)?
import java.util.*;
import java.io.*;
Java Code:class mainprogram { //These are available for the whole class static InputStreamReader osw = new InputStreamReader (System.in, "Cp850"); ... }
They want me declare the exception, but how? I know how to do it if it was inside a method, but outside a method?Java Code:C:\Kryptator\mainprogram.java:9: unreported exception java.io.UnsupportedEncodingException; must be caught or declared to be thrown static InputStreamReader osw = new InputStreamReader (System.in, "Cp850"); ^ 1 error Tool completed with exit code 1
- 08-04-2011, 07:43 PM #2
- 08-05-2011, 03:35 PM #3
New "cant compile" issue. This one dont make sense, it seems like the compiler is blind.
PHP Code:Loop1: while (jtemp <= bigFile.length ()) { FLoop: while (false) { finalString = finalString.append (temp); itemp += 12; jtemp += 12; continue Loop1; } temp = bigFile.toString ().substring (itemp, jtemp); //DEBUG System.out.println (temp); System.gc (); for (int j = 0; j < theChars.length + 2; j++) { Loop2: for (int l = 0; l < 100; l++) { if (temp.equals (key[j][l])) { if (j == theChars.length) { temp = temp.replace (temp, "[unsupported character]"); continue FLoop; } if (j == theChars.length + 1) { temp = temp.replace (temp, "\n"); continue FLoop; } tempChar = theChars[j] + ""; temp = temp.replace (temp, tempChar); continue FLoop; } else continue Loop2; } } }
Java Code:C:\Program\Java\jdk1.6.0_23\jre\classes\pjjava\crypt\cryptMethods.java:255: undefined label: FLoop continue FLoop; ^ C:\Program\Java\jdk1.6.0_23\jre\classes\pjjava\crypt\cryptMethods.java:261: undefined label: FLoop continue FLoop; ^ C:\Program\Java\jdk1.6.0_23\jre\classes\pjjava\crypt\cryptMethods.java:265: undefined label: FLoop continue FLoop; ^ 3 errors Tool completed with exit code 1
- 08-05-2011, 04:00 PM #4
As good an example of goto code as I've seen in years.
The continue FLoop is not within a loop where FLoop: is defined. FLoop is just floating around inside of the outer loop. FLoop needs to be on an outer loop that contains the continue.
- 08-05-2011, 05:07 PM #5
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
- 08-06-2011, 12:01 AM #6
Not necessarily.
That part is correct.
Now why was I dumb enough to search through the JLS and post a link to the section that accurately describes how to deal with checked exceptions from an instance initializer, hoping someone would actually click the link and learn something in the process.
db
- 08-06-2011, 01:01 AM #7
- 08-06-2011, 01:44 AM #8
Basically you need to move the FLoop label to be at the beginning of a loop that contains the continue FLoop statement. The loop where it is now does not contain the continue FLoop statement.
You are trying to 'goto' the FLoop statement.
For more on goto, ask Google
- 08-06-2011, 01:55 AM #9
Member
- Join Date
- Aug 2011
- Posts
- 95
- Rep Power
- 0
You are correct, good sir.
For the readers, see "8.6 Instance Initializers" by Following This Link (and scroll up just one click.)
Still, to throw the checked exception with Instance Initializers, one would have to have at least one constructor, and to declare the checked exception in all of them.
To not throw the checked exception, the instance initializer block would have to have a try-catch block that did "something appropriate" with the exception -- whatever that might be. (Print stack trace and eat it? Wrap it in a RuntimeException?)
Personally, I avoid static and instance initializer blocks, except in extreme circumstances. And when they are the best solution to the problem, I usually find that the code really needs to be refactored so as to not have that kind of problem anyway.
Given the original example, this is what an initializer block would look like:
Java Code:import java.io.*; class mainprogram { static InputStreamReader osw; static { try { osw = new InputStreamReader(System.in, "Cp850"); } catch (UnsupportedEncodingException ex) { ex.printStackTrace(); } } }Last edited by JeffGrigg; 08-06-2011 at 02:01 AM. Reason: Fix [CODE] markup, and URL link.
Similar Threads
-
why i can not compile? please help
By zypchun in forum New To JavaReplies: 3Last Post: 04-20-2011, 05:48 PM -
would this compile?
By stringkilla in forum New To JavaReplies: 10Last Post: 10-24-2010, 03:27 PM -
Can't Compile
By sidk47 in forum JavaServer Pages (JSP) and JSTLReplies: 7Last Post: 06-15-2010, 04:43 PM -
Help with compile
By mr_anderson in forum NetBeansReplies: 7Last Post: 06-10-2010, 04:03 AM -
Not able to compile
By bugger in forum New To JavaReplies: 2Last Post: 01-09-2008, 10:13 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks