Could you tell me when to use throw, exit and error?
1> catch throw ({aaa}).
{aaa}
2> catch exit ({aaa}).
{'EXIT',{aaa}}
3> catch gen_server:call(aaa,{aaa}).
{'EXIT',{noproc,{gen_server,call,[aaa,{aaa}]}}}
4> catch exit("jaj")
{'EXIT',"jaj"}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are 3 classes which can be caught with a
try ... catch:throw,errorandexit.throwis generated usingthrow/1and is intended to be used for non-local returns and does not generate an error unless it is not caught (when you get anocatcherror).erroris generated when the system detects an error. You can explicitly generate an error usingerror/1. The system also includes a stacktrace in the generated error value, for example{badarg,[...]}.exitis generated usingexit/1and is intended to signal that this process is to die.The difference between
error/1andexit/1is not that great, it more about intention which the stacktrace generated by errors enhances.The difference between them is actually more noticeable when doing
catch ...: whenthrow/1is used then thecatchjust returns the thrown value, as is expected from a non-local return; when anerror/1is used then thecatchreturns{'EXIT',Reason}whereReasoncontains the stacktrace; while fromexit/1catchalso returns{'EXIT',Reason}butReasononly contains the actual exit reason.try ... catchlooks like it equates them, but they are/were very different.