从 if 块中抛出错误,也会导致其他语句的执行
Throwing an error from the if block, results into execution of other statements as well
我正在编写代码来验证表单的字段。因此,我编写了一个名为 validateFields 的函数来执行此操作。因此,如果所有字段都正确,则该函数将 return true else false。 validateFields 函数在另一个名为 saveForm 的函数中调用。 saveForm 函数的目的是仅当 validateFields return 为真时才保存数据。 saveForm 已经在 try 块中执行了另一个 promise 回调,如果 promise 失败,则会出现 return 错误。
我想要实现的是,如果 validateFields return false 那么我想在 saveForm 函数中抛出一个错误。
// this function is present in another file say validation.js
function validateForm(){
//some logic to validate which returns true or false;
}
//this function is present in another file say saveForm.js, the structure of saveForm function is as follows
function saveForm(){
var isFormValid = validation.validateForm(); //validateForm function invoked
try{
//some existing logic to send the saved form
}catch(error){
console.log(error)
}
}
现在,如果 isFormValid 为 false,那么我不想在 try 块中执行现有逻辑,而是抛出一个错误,指出表单验证失败。我尝试通过以下方式进行但失败了
function saveForm(){
try{
var isFormValid = validation.validateForm(); //validateForm function invoked
try{
//some existing logic to send the saved form
}catch(error){
console.log(error)
}catch(error){
console.log("Validation failed error");
}
}
}
我只想执行外部 catch 块,但相反,我从两个 catch 块获取消息,所以任何人都可以帮助我如何放置 isValidForm 逻辑,以便我只收到验证失败错误?
每个try
!
后面应该有一个catch or finally
按照你的逻辑,最后一次捕获应该在第一次尝试之外,以捕获它的错误,即使那样也没有 if
语句来检查 isFormValid
试试这个逻辑:
function saveForm() {
var isFormValid = validation.validateForm(); //validateForm function
try {
if (!isFormValid) {
throw "Form is invalid";
} else {
try {
//some existing logic to send the saved form
console.log("form save logic");
// throw error if something goes wrong. Only for test purpose
throw "Error while saving";
} catch (error) {
console.log(error);
}
}
} catch (error) {
console.log(error);
}
}
或者这个,IMO 哪个更具可读性
function saveForm() {
var isFormValid = validation.validateForm(); //validateForm function
if (isFormValid) {
try {
//some existing logic to send the saved form
console.log("form save logic");
// throw error if something goes wrong. Only for test purpose
throw "Error while saving";
} catch (error) {
console.log(error);
}
} else {
try{
throw "Form is invalid";
}
catch(error){
console.log(error);
}
}
}
我正在编写代码来验证表单的字段。因此,我编写了一个名为 validateFields 的函数来执行此操作。因此,如果所有字段都正确,则该函数将 return true else false。 validateFields 函数在另一个名为 saveForm 的函数中调用。 saveForm 函数的目的是仅当 validateFields return 为真时才保存数据。 saveForm 已经在 try 块中执行了另一个 promise 回调,如果 promise 失败,则会出现 return 错误。 我想要实现的是,如果 validateFields return false 那么我想在 saveForm 函数中抛出一个错误。
// this function is present in another file say validation.js
function validateForm(){
//some logic to validate which returns true or false;
}
//this function is present in another file say saveForm.js, the structure of saveForm function is as follows
function saveForm(){
var isFormValid = validation.validateForm(); //validateForm function invoked
try{
//some existing logic to send the saved form
}catch(error){
console.log(error)
}
}
现在,如果 isFormValid 为 false,那么我不想在 try 块中执行现有逻辑,而是抛出一个错误,指出表单验证失败。我尝试通过以下方式进行但失败了
function saveForm(){
try{
var isFormValid = validation.validateForm(); //validateForm function invoked
try{
//some existing logic to send the saved form
}catch(error){
console.log(error)
}catch(error){
console.log("Validation failed error");
}
}
}
我只想执行外部 catch 块,但相反,我从两个 catch 块获取消息,所以任何人都可以帮助我如何放置 isValidForm 逻辑,以便我只收到验证失败错误?
每个try
!
catch or finally
按照你的逻辑,最后一次捕获应该在第一次尝试之外,以捕获它的错误,即使那样也没有 if
语句来检查 isFormValid
试试这个逻辑:
function saveForm() {
var isFormValid = validation.validateForm(); //validateForm function
try {
if (!isFormValid) {
throw "Form is invalid";
} else {
try {
//some existing logic to send the saved form
console.log("form save logic");
// throw error if something goes wrong. Only for test purpose
throw "Error while saving";
} catch (error) {
console.log(error);
}
}
} catch (error) {
console.log(error);
}
}
或者这个,IMO 哪个更具可读性
function saveForm() {
var isFormValid = validation.validateForm(); //validateForm function
if (isFormValid) {
try {
//some existing logic to send the saved form
console.log("form save logic");
// throw error if something goes wrong. Only for test purpose
throw "Error while saving";
} catch (error) {
console.log(error);
}
} else {
try{
throw "Form is invalid";
}
catch(error){
console.log(error);
}
}
}