首页 > 代码库 > 返回flag
返回flag
//修改前
namespace CleanCSharp.Errors.Dirty { public class SomeClass { public int DoSomeProcess(int? id) { if (id == null) { return -1; // null id } string data =http://www.mamicode.com/ LoadData(); if (string.IsNullOrWhiteSpace(data)) { return -2; // data is corrupt } ProcessData(data); return 0; // no error, all good } private string LoadData() { return "some data"; } private void ProcessData(string data) { // do something } } }
修改前,调用 namespace CleanCSharp.Errors.Dirty { public class ConsumerOfSomeClass { public void Consume() { var sc = new SomeClass(); const int idToProcess = 42; int returnCode = sc.DoSomeProcess(idToProcess); switch (returnCode) { case -1: // null id // do something break; case -2: // corrupt data // do something break; case 0: // no error Save(idToProcess); break; } } private void Save(int id) { // save } } }
//修改后 using System; using System.IO; namespace CleanCSharp.Errors.Clean { public class SomeClass { public void DoSomeProcess(int? id) { if (id == null) { throw new ArgumentNullException("id"); } string data =http://www.mamicode.com/ LoadData(); ProcessData(data); } private string LoadData() { var demoData = http://www.mamicode.com/""; if (string.IsNullOrWhiteSpace(demoData)) { throw new InvalidDataException( "The data stream contains no data."); } return demoData; } private void ProcessData(string data) { // do something } } }
//修改后 using System; using System.Diagnostics; using System.IO; namespace CleanCSharp.Errors.Clean { public class ConsumerOfSomeClass { public void Consume() { var sc = new SomeClass(); const int idToProcess = 42; try { sc.DoSomeProcess(idToProcess); } catch (ArgumentNullException ex) { // null id // do something such as logging // if cannot respond to this // exception propagate up the // call stack throw; // Notice the throw is not: throw ex; } catch (InvalidDataException ex) { // bad data // do something throw; } catch (Exception ex) { // any other exceptions that may occur // do something throw; } Save(idToProcess); } private void Save(int id) { // save } } }
返回flag
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。