Unity热更新方案C#Like(十四)-详解支持的C#特性:异常处理

C#Like是Unity的热更方案,使用纯C#语言写出可以热更新的代码,就像可以在所有平台使用DLL(动态链接库)文件一样.遵从KISS设计原则,让用户轻松构建或升级成Unity的热更新项目.

简介

本篇主要介绍C#Like支持的C#特性:异常处理

  • C#Like免费版:throw;
  • C#Like完整版:多了try-catch-finally.

C#Like免费版:

示范代码如下,提供对应方案模拟:

using UnityEngine;  
  
namespace CSharpLike  
{  
    /// <summary>  
    /// Example test C# function in hot update script.  
    /// </summary>  
    public partial class SampleCSharp : LikeBehaviour  
    {  
        void TestException()  
        {  
            Debug.LogError("测试异常: 免费版不支持 'try-catch-finally' (在完整版支持). ");  
            //throw new Exception("test throw except");//可以使用throw关键字  
        }  
    }  
}  

 C#Like完整版:

示范代码如下:

using UnityEngine;  
using System;  
  
namespace CSharpLike  
{  
    public partial class SampleCSharp : LikeBehaviour  
    {  
        void TestException()  
        {  
            Debug.LogError("Test Exception:");  
            //测试 try {} catch (Exception e)  {} finally {}  
            try  
            {  
                Debug.Log("test try step 1");  
                CrashHere();  
                Debug.Log("test try step 2");//不会进来这里  
            }  
            catch (Exception e)  
            {  
                //控制台输出异常堆栈信息(含类名,函数名,行数).  
                //Error msg:Non-static method requires a target. Stack info:  
                //class:SampleCSharp function:CrashHere() file:\ExampleC#_Exception.cs line:71  
                //class:SampleCSharp function:TestException() file:\ExampleC#_Exception.cs line:22  
                //class:SampleCSharp function:Start() file:\ExampleC#.cs line:28  
                Debug.LogError("catch error:" + e);  
            }  
            finally  
            {  
                Debug.Log("test try step 3");  
            }  
  
            //测试 try {} catch {}  
            try  
            {  
                Debug.Log("test try step 4");  
                int x = 100;  
                int y = 1 / (x - 100);//除以0,这里会报错  
                Debug.Log("test try step 5");//不会进来这里  
            }  
            catch//没有(Exception e)的方式  
            {  
                Debug.LogError("catch error");  
            }  
  
            try  
            {  
                //测试 try {} finally {},这种模式不会捕捉异常,异常会继续直到被捕捉  
                try  
                {  
                    Debug.Log("test try step 6");  
                    ThrowExcept();  
                    Debug.Log("test try step 7");//不会到达这里  
                }  
                finally  
                {  
                    Debug.Log("test try step 8");//会输出这个  
                }  
            }  
            catch  
            {  
                Debug.Log("test try step 9");//会输出这个  
            }  
        }  
        void CrashHere()  
        {  
            GameObject go = null;  
            go.GetComponent<HotUpdateBehaviour>();//空对象,会报错  
            Debug.Log("won't reach here");  
        }  
  
        void ThrowExcept()  
        {  
            Debug.Log("test try ThrowExcept");  
            throw new Exception("test throw except");//测试抛出异常  
        }  
    }  
}

 本系列文章导读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C#Like

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值