简体   繁体   English

如何跳过 Java/Processing 中出现语法错误的行?

[英]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. function 的第一行显然存在语法问题(即 pow() 中没有足够的 arguments 和一个不合适的逗号),因此代码将无法编译。 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.我希望有一些东西,可能类似于try ... catch块,可以简单地跳过这个不可用的行并继续下一个。

Thanks in advance!提前致谢!

Just for reference, I'm using Processing 4.0 on Windows 10.仅供参考,我在 Windows 10 上使用 Processing 4.0。

Yeah to my knowledge, try-catch block is your best bet here.是的,据我所知,try-catch 模块是您最好的选择。 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.您可以在 Try/Except 中使用执行代码(使用 Bash 代码或任何执行任务的代码)。 A example (Pseudo-code because i don't know Java)一个例子(伪代码,因为我不懂 Java)

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

Suggestion: Run you program, check output using javac, look at the exit code.建议:运行你的程序,用javac检查output,看看退出码。 If non zero, rerun your code generation code with more strict output if that is possible.如果非零,如果可能的话,使用更严格的 output 重新运行您的代码生成代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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