Will CLR handle both CLS-Complaint and non-CLS complaint exceptions?

前端 未结 3 676
眼角桃花
眼角桃花 2020-12-16 21:28

Just for my clarification :

Can i throw both CLS-Complaint and non-CLS complaint exceptions in .NET Framework ?.I am using C# 3.0.

When i catch exception

3条回答
  •  孤城傲影
    2020-12-16 22:20

    Although the C# compiler allows developers to throw Exception-derived objects only, prior to C# version 2.0, the C# compiler did allow developers to catch non-CLS-compliant exceptions by using code like this:

    private void SomeMethod() { 
    try { 
    // Inside the try block is where you put code requiring 
    // graceful recovery or common cleanup operations. 
    }
    catch (Exception e) { 
    // Before C# 2.0, this block catches CLS-compliant exceptions only 
    // In C# 2.0, this block catches CLS- & non-CLS- compliant exceptions 
    throw; // Re-throws whatever got caught 
    }
    catch { 
    // In all versions of C#, this block catches 
    // CLS- & non-CLS- compliant exceptions 
    throw; // Re-throws whatever got caught 
    }
    }
    

提交回复
热议问题