Possible Bug of loading Package @ Mathematica 10.1

When I open mathematica, there will pop-up some messages

where

<< package


is in the following file

FindFile["init.m"]

(*
C:\Users\HyperGroups\AppData\Roaming\Mathematica\Kernel\init.m
*)


package content in the package file:

BeginPackage["Test1"]

Test1::usage = "";

Begin["Private"];

Nearest[{1, 2, 3, 5, 8, 5}, 5]

End[]

EndPackage[]


I've tried * Mathematica * 10.1@Windows and 10.0[without this problem]

Seems the problem is related with some special functions, like Nearest

• Problem confirmed in 10.1.0 under Windows 7 x64. – Mr.Wizard May 27 '15 at 14:45
• should this be tagged as a bug? – P. Fonseca May 28 '15 at 5:38
• Certainly it is one, but a fix would be (very) non-trivial. I tend to view it as more of a limitation. – ilian May 28 '15 at 14:18

1 Answer

Update

This has been fixed in a general way as of version 11.2.0. For more details, see this answer.

This is a known issue. It is not about package loading (or Nearest) but about Catch/Throw not working correctly when used during kernel initialization. For example, just putting the following in init.m

Catch[Throw[1]]


will result in similar behavior. See also Arnoud Buzing's post here.

The workaround I've been using is to run any init code depending on Catch/Throw as a scheduled task, e.g.

task := Get["package"];
RunScheduledTask[task, {1}]

• I must be missing something... nowhere in the OP's code is there a call to Catch or Throw. Should I understand that there are calls to these functions inside Nearest? – QuantumDot May 25 '16 at 9:01
• @QuantumDot Yes, see e.g. TracePrint[Nearest[{1, 2, 4, 8, 16, 32}, 20], _Catch, TraceInternal -> True]` – ilian May 25 '16 at 15:19