JLink或UseFrontEnd生成的未捕获的抛出 [英] Uncaught Throw generated by JLink or UseFrontEnd

查看:74
本文介绍了JLink或UseFrontEnd生成的未捕获的抛出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此示例例程在内核窗口中生成两条Throw :: nocatch警告消息.可以以某种方式处理它们吗?

This example routine generates two Throw::nocatch warning messages in the kernel window. Can they be handled somehow?

该示例包含以下代码,这些代码位于C:\ Temp中创建的文件"test.m"中:

The example consists of this code in a file "test.m" created in C:\Temp:

Needs["JLink`"];
$FrontEndLaunchCommand = "Mathematica.exe";
UseFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];

然后将这些命令粘贴并在Windows命令提示符处运行:

Then these commands pasted and run at the Windows Command Prompt:

PATH = C:\Program Files\Wolfram Research\Mathematica\8.0\;%PATH%
start MathKernel -noprompt -initfile "C:\Temp\test.m"

附录

使用UseFrontEnd而不是UseFrontEnd的原因是,可能需要交互式前端来保留通常以交互方式运行的笔记本的输出和消息.例如,像这样修改C:\ Temp \ test.m:

The reason for using UseFrontEnd as opposed to UsingFrontEnd is that an interactive front end may be required to preserve output and messages from notebooks that are usually run interactively. For example, with C:\Temp\test.m modified like so:

Needs["JLink`"];
$FrontEndLaunchCommand="Mathematica.exe";
UseFrontEnd[
nb = NotebookOpen["C:\\Temp\\run.nb"];
SelectionMove[nb, Next, Cell];
SelectionEvaluate[nb];
];
Pause[10];
CloseFrontEnd[];

,然后创建一个笔记本C:\ Temp \ run.nb,其中单个单元格包含:

and a notebook C:\Temp\run.nb created with a single cell containing:

x1 = 0; While[x1 < 1000000,
 If[Mod[x1, 100000] == 0,
  Print["x1=" <> ToString[x1]]]; x1++];
NotebookSave[EvaluationNotebook[]];
NotebookClose[EvaluationNotebook[]];

此代码是从Windows命令提示符启动的,将以交互方式运行并保存其输出.使用UsingFrontEnd或MathKernel -script"C:\ Temp \ test.m"无法实现.

this code, launched from a Windows Command Prompt, will run interactively and save its output. This is not possible to achieve using UsingFrontEnd or MathKernel -script "C:\Temp\test.m".

推荐答案

在初始化期间,内核代码处于防止中止的模式.

During the initialization, the kernel code is in a mode which prevents aborts.

Throw/Catch是通过Abort实现的,因此它们在初始化期间不起作用.

Throw/Catch are implemented with Abort, therefore they do not work during initialization.

一个显示问题的简单示例是将其放在您的test.m文件中:

A simple example that shows the problem is to put this in your test.m file:

Catch[Throw[test]];

类似地,TimeConstrained,MemoryConstrained,Break,Trace家族,Abort以及依赖于它的函数(例如某些数据包)之类的函数在初始化期间也会遇到类似问题.

Similarly, functions like TimeConstrained, MemoryConstrained, Break, the Trace family, Abort and those that depend upon it (like certain data paclets) will have problems like this during initialization.

可能的解决方案是考虑使用-script选项:

A possible solution to your problem might be to consider the -script option:

math.exe -script test.m

另外,请注意,在版本8中,有一个文档记录的函数称为UsingFrontEnd,该函数执行UseFrontEnd的功能,但会自动配置,因此:

Also, note that in version 8 there is a documented function called UsingFrontEnd, which does what UseFrontEnd did, but is auto-configured, so this:

Needs["JLink`"];
UsingFrontEnd[NotebookWrite[CreateDocument[], "Testing"]];

应该是test.m文件中所需的全部内容.

should be all you need in your test.m file.

另请参阅: Mathematica脚本

附录

使用-script和UsingFrontEnd的一种可能的解决方案是使用'run.m脚本 包括在下面.这确实需要在内核配置选项中设置测试"内核(基本上是本地"内核设置的克隆).

One possible solution to use the -script and UsingFrontEnd is to use the 'run.m script included below. This does require setting up a 'Test' kernel in the kernel configuration options (basically a clone of the 'Local' kernel settings).

该脚本包括两个实用程序功能,NotebookEvaluatingQ和NotebookPauseForEvaluation,它们可以帮助脚本等待客户端笔记本完成评估之后再保存.这种方法的好处是所有评估控制代码都在'run.m'脚本中,因此客户端笔记本不需要在末尾使用NotebookSave [EvaluationNotebook []]语句.

The script includes two utility functions, NotebookEvaluatingQ and NotebookPauseForEvaluation, which help the script to wait for the client notebook to finish evaluating before saving it. The upside of this approach is that all the evaluation control code is in the 'run.m' script, so the client notebook does not need to have a NotebookSave[EvaluationNotebook[]] statement at the end.

NotebookPauseForEvaluation[nb_] := Module[{},While[NotebookEvaluatingQ[nb],Pause[.25]]]

NotebookEvaluatingQ[nb_]:=Module[{},
SelectionMove[nb,All,Notebook];
Or@@Map["Evaluating"/.#&,Developer`CellInformation[nb]]
]

UsingFrontEnd[
nb = NotebookOpen["c:\\users\\arnoudb\\run.nb"];
SetOptions[nb,Evaluator->"Test"];
SelectionMove[nb,All,Notebook];
SelectionEvaluate[nb];
NotebookPauseForEvaluation[nb];
NotebookSave[nb];
]

我希望这对您有所帮助.它可以使用其他一些改进,例如将笔记本的内核重置为原始内核,并在保存后关闭笔记本, 但是此代码应为此特定目的而工作.

I hope this is useful in some way to you. It could use a few more improvements like resetting the notebook's kernel to its original and closing the notebook after saving it, but this code should work for this particular purpose.

在旁注中,我尝试了另一种方法,使用此方法:

On a side note, I tried one other approach, using this:

UsingFrontEnd[ NotebookEvaluate[ "c:\\users\\arnoudb\\run.nb", InsertResults->True ] ]

但这会将内核终端会话踢入对话模式,这似乎是一个错误 给我(如果这是有效问题,我会进行检查并得到报告).

But this is kicking the kernel terminal session into a dialog mode, which seems like a bug to me (I'll check into this and get this reported if this is a valid issue).

这篇关于JLink或UseFrontEnd生成的未捕获的抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆