设为首页 加入收藏

TOP

错误处理(Operation Result)方法(二)
2019-09-03 02:18:03 】 浏览:27
Tags:错误 处理 Operation Result 方法
ReadAllBytes 方法变为

public OperationResult<byte[]> TryReadAllBytes(string path)
{
    try
    {
        var bytes = File.ReadAllBytes(path);
        return OperationResult<byte[]>.CreateSuccessResult(bytes);
    }
    catch (FileNotFoundException fileNotFoundException)
    {
        return OperationResult<byte[]>.CreateFailure(fileNotFoundException);
    }
}

调用代码:

var result = myStorageService.TryReadAllBytes(path);
if(result.Success)
{
    // do something
}
else
{
    Logger.Log(String.Format("Failed to read file from path, {0}: {1}", path, result.NonSuccessMessage));
}

 

原文:Error Handling in SOLID C# .NET – The Operation Result Approach

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【.NET深呼吸】如何反序列化动态J.. 下一篇C#之#if #endif的简单用法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目