小编Ler*_*tai的帖子

kotlin 异步异常处理

鉴于以下代码段,我不明白为什么我的 android 应用程序崩溃。我在独立的 kotlin 应用程序中进行了测试,但这不会发生。

class LoginActivity : AppCompatActivity(), CoroutineScope
{
     lateinit var job: Job
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job


   override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        job = Job()

        try
        {
            launch()
            {
                try
                {
                    var res = async { test() }

                    res.await()

                } 
                catch (e2: java.lang.Exception)
                {

                }
            }

        }
        catch (e: java.lang.Exception)
        {


        }
    }

    fun test(): String
    {
        throw java.lang.Exception("test ex")
        return "";
    }
}


 --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main …
Run Code Online (Sandbox Code Playgroud)

crash android asynchronous coroutine kotlin

5
推荐指数
1
解决办法
1万
查看次数

try catch块中的对象

拿这个代码示例:

string status = "ok";
SqlCommand cmd=null;
SqlTransaction trans=null;
try
{
    cmd = defs.prepquery("");
    trans = cmd.Connection.BeginTransaction();
    cmd.Transaction = trans;

}
catch (Exception ex)
{
    status = defs.logerror("initalizing sql transaction:" + ex.ToString());
    return status;
}

try
{
    if (oper == "submit")
    {
        cmd.CommandText = "update DCM_Mapping_Sessions set StatusID=2 " + 
            "where MappingSessionID=" + mpsid + "";
        cmd.ExecuteNonQuery();
    }
    else if (oper == "delete")
    {
        // .......etc etc 
    }
    catch(Exception ex2)
    {
        //rollback , close the connection
       // handle the ex …
Run Code Online (Sandbox Code Playgroud)

c# error-handling try-catch

1
推荐指数
1
解决办法
801
查看次数