Fred wrote:
> Is it possible to use throw in javascript without try..catch? As far
> as I know, you must call it from within a try..catch block, or the
> function that calls throw must itself be called from within try..catch,
> e.g.:
>
You can throw exceptions from just about anywhere. Look at this example
from the docs...
function UserException (message) {
this.message=message;
this.name="UserException";
}
function getMonthName (mo) {
mo=mo-1; // Adjust month number for array index (1=Jan, 12=Dec)
var months=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul",
"Aug","Sep","Oct","Nov","Dec");
if (months[mo] != null) {
return months[mo];
} else {
myUserException=new UserException("InvalidMonthNo");
throw myUserException;
}
}
............
try {
// statements to try;
monthName=getMonthName(myMonth)
}
catch (e) {
monthName="unknown";
logMyErrors(e.message,e.name); // pass exception object to err
handler
}
Regards, crater
|