2017-12-15 8 views
-1

tryブロックが失敗した場合、私のコードに関数に関するエラーをスローしたいと思っています。ここコードがcatchブロックasp.netで動作していない場合はエラーメッセージをスローする

は私のコードは、私はasp.net

更新

public JsonResult SaveRecordForFiberEng(FiberDataInsertion FiberDataInsertion, string myJsonXML) 
    { 
     string strFiber = ""; 
     try 
     { 
      FiberEngineer ObjFiber = new FiberEngineer(); 
      strFiber = ObjFiber.UpdateFiberEngRecord(FiberDataInsertion, myJsonXML); 

      if (strFiber.Split('|')[0] == "SUCCESS") 
      { 
       SendEmail(FiberDataInsertion.MODIFIED_BY, FiberDataInsertion.UMS_GROUP_ASS_TO_ID, FiberDataInsertion.UMS_GROUP_ASS_TO_NAME, FiberDataInsertion.UMS_GROUP_ASS_BY_NAME, FiberDataInsertion.REQUEST_STATUS_TYPE, FiberDataInsertion.PROG_ID); 
      } 

      return Json(strFiber, JsonRequestBehavior.AllowGet); 
     } 
     catch (Exception ex) 
     { 
      new Exception(strFiber.Split('|')[0] + " ERROR| Error ocurred on Approve, Reject & Save Data"); 
     } 
    } 
+0

@mjwills:それは適切ですcatch(Exception ex) { throw new Exception(strFiber.Split( '|')[0]); } ' – BNN

+0

@mjwills:どのようにチェックすればいいのですか?@mjwills:どうすればいいですか? – BNN

+0

@mjwills:どうすればいいですか? – BNN

答えて

1

一つのアプローチが検討する:

string strFiber = null; 
try 
{ 
    FiberEngineer ObjFiber = new FiberEngineer(); 
    strFiber = ObjFiber.UpdateFiberEngRecord(FiberDataInsertion, myJsonXML); 

    if (strFiber.Split('|')[0] == "SUCCESS") 
    { 
     SendEmail(FiberDataInsertion.MODIFIED_BY, FiberDataInsertion.UMS_GROUP_ASS_TO_ID, FiberDataInsertion.UMS_GROUP_ASS_TO_NAME, FiberDataInsertion.UMS_GROUP_ASS_BY_NAME, FiberDataInsertion.REQUEST_STATUS_TYPE, FiberDataInsertion.PROG_ID); 
    } 

    return Json(strFiber, JsonRequestBehavior.AllowGet); 
} 
catch (Exception ex) 
{ 
    var fiber = strFiber == null ? null : strFiber.Split('|').FirstOrDefault(); 
    throw new Exception($"fiber value was {fiber}"); 
} 

キーはtry..catchの外strFiberを宣言しています。また、catchブロック内のstrFiber(まだnullの場合)には注意してください。

+0

'throw'を使わずに書くことができますか? – BNN

+0

あなたの質問**明示的に**あなたが_投げたいと述べています。 _しかし、あなたは**投げる必要はありません**。私の「スロー」行をあなたが望むものに変更してください。 – mjwills

0

でこれを行う必要がありますどのように

try 
{ 
    FiberEngineer ObjFiber = new FiberEngineer(); 
    string strFiber = ObjFiber.UpdateFiberEngRecord(FiberDataInsertion, myJsonXML); 

    if (strFiber.Split('|')[0] == "SUCCESS") 
    { 
     SendEmail(FiberDataInsertion.MODIFIED_BY, FiberDataInsertion.UMS_GROUP_ASS_TO_ID, FiberDataInsertion.UMS_GROUP_ASS_TO_NAME, FiberDataInsertion.UMS_GROUP_ASS_BY_NAME, FiberDataInsertion.REQUEST_STATUS_TYPE, FiberDataInsertion.PROG_ID); 
    } 

    return Json(strFiber, JsonRequestBehavior.AllowGet); 
} 
catch (Exception ex) 
{ 
    //error message related to this string "strFiber" 
} 

であるここに私達は行く: catchブロックでこれを入れて:

throw new HttpStatusCodeResult(HttpStatusCode.BadRequest); // returns 400 

本文テキストを含める場合は、ViewResultを使用し、Response.StatusCodeと本文を含むビューを設定します。 また、ログファイルに例外を記録することもできます。 コードはcatchブロックでそのようsomethigます:

logger.Error(ex.Message); 
+0

上記のコードは私に何を表示するのですか? – BNN

関連する問題