RE: Re: How to use error message return values
- To: mathgroup at smc.vnet.net
- Subject: [mg37772] RE: [mg37758] Re: How to use error message return values
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Tue, 12 Nov 2002 03:13:31 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: daiyanh at earthlink.net [mailto:daiyanh at earthlink.net] To: mathgroup at smc.vnet.net >Sent: Monday, November 11, 2002 11:11 AM >To: mathgroup at smc.vnet.net >Subject: [mg37772] [mg37758] Re: How to use error message return values > > >In article <aqld31$bhn$1 at smc.vnet.net>, Andrzej Kozlowski ><andrzej at tuins.ac.jp> wrote: > >>One possibility: >> >>inv[M_, p_] := Block[{$Messages = {}}, Check[Inverse[M, Modulus -> p], >>"Your matrix has no inverse modulo " <> ToString[p] <> ". Use another one."]] > >Hmmm...This Check function does not "halt" the computation... > >In[]:= >Check[ > Inverse[{{5, 5}, {5, 5}}, Modulus -> 26]; > Print[123], > $Failed] > >--> > >Inverse::modp: > Value of option Modulus -> 26 should be a prime number. > >123 > >Out[]= >$Failed > > >Some more test: > >Unprotect[Message]; >Message[Inverse::modp,__]:=Throw[$Failed]; >Protect[Message]; > >Catch[ > Inverse[{{5, 5}, {5, 5}}, Modulus -> 26]; > 123 >] >--> 123 > > >So Throw doesn't work either. > > >Likewise for Return as well. > > >DH > What is wrong with In[11]:= inv::"sing" = "Matrix has no inverse modulo `1`"; In[12]:= inv[m_, p_] := Check[Inverse[m, Modulus -> 26], Message[inv::"sing", 26], Inverse::"sing"] In[13]:= inv[{{5, 5}, {5, 5}}, 26] >From In[13]:= Inverse::"sing": "Matrix {{5, 5}, {5, 5}} is singular." >From In[13]:= inv::"sing": "Matrix has no inverse modulo 26" >From Help: "Check[expr, failexpr] evaluates expr, and returns the result, unless messages were generated, in which case it evaluates and returns failexpr." In[16]:= Check[Inverse[c, Modulus -> 26]; Print["oops"]; Pi, Message[inv::"sing", 26], Inverse::"sing"] >From In[16]:= Inverse::"sing": "Matrix {{5, 5}, {5, 5}} is singular." >From In[16]:= "oops" >From In[16]:= inv::"sing": "Matrix has no inverse modulo 26" So... expr == Inverse[c, Modulus -> 26]; Print["oops"]; Pi ...was evaluated, Pi was *not* returned, but failexpression... In[17]:= Message[inv::"sing", 26] >From In[17]:= inv::"sing": "Matrix has no inverse modulo \!\(26\)" In[18]:= % // InputForm Out[18]//InputForm= Null ...was returned However... In[23]:= %16 // InputForm Out[23]//InputForm= Pi ...will give you the remains of expr (normally nonsense). So if you want to abort some calculation in case of error, just *only* include that into Check! So this might be something you liked (e.g.): In[45]:= Catch[Check[Inverse[a, Modulus -> 26], Message[inv::"sing", 26]; Throw[$Failed], Inverse::"sing"]; Print["oops"]; Pi] >From In[45]:= "oops" Out[45]= Pi In[46]:= Catch[Check[Inverse[c, Modulus -> 26], Message[inv::"sing", 26]; Throw[$Failed], Inverse::"sing"]; Print["oops"]; Pi] >From In[46]:= Inverse::"sing": "Matrix {{5, 5}, {5, 5}} is singular." >From In[46]:= inv::"sing": "Matrix has no inverse modulo 26" Out[46]= $Failed -- Hartmut Wolf