简体   繁体   中英

How to skip a line with a syntax error in Java/Processing?

I have a program that generates a bunch of code snippets, most of which are syntactically usable, but some of which are not. The ones that don't might look like

return 2.0+0.0+pow(2.0),2.0;

I have been unable to modify my original code to avoid these issues, but I suppose this could be fixed by some method to test if a string contains syntactically correct code.

Here's an example of my code. This entire file would be saved from another program to be run afterwards.

{
print(myFun());
}

float myFun() {
  return 2.0+0.0+pow(2.0),2.0;
  return 10;
}

The first line of the function obviously has syntax issues (ie, not enough arguments in pow() and an out-of-place comma), so the code will not compile. My hope would be that there is something, possibly similar to a try ... catch block, to simply skip over this unusable line and move on to the next.

Thanks in advance!

Just for reference, I'm using Processing 4.0 on Windows 10.

Yeah to my knowledge, try-catch block is your best bet here. There might be someone else though that might know a different way

You can use execute code (Using Bash code or anything that does the task) with Try/Except. A example (Pseudo-code because i don't know Java)

...
try
execute return 5
except
pass
end
...

Suggestion: Run you program, check output using javac, look at the exit code. If non zero, rerun your code generation code with more strict output if that is possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM