2010-01-21 60 views

回答

2

MS的SharePoint拋出異常是這樣的:

.... 
Throw New SPException(SPResource.GetString("CannotChangeRootwebPerm", New Object() { Me.Url })) 
.... 

聽起來合理。 - 你有一個實用功能,它知道如何找到當前語言環境所需的信息;您給每個異常字符串一個可讀的&邏輯名稱。

+0

+1爲簡單起見。簡單很好。 – Walter 2010-01-21 13:52:49

0

我會總是喜歡例外而不是任意的返回值。 創建multilinguar異常的方法是創建一個基類,它負責翻譯的,即:

class TranslatedException : Exception { 
    private string translationKey = "base_exception"; 
    public string ToString() { 
     return YourTranslationClass.GetTranslation(this.translationKey); 
    } 
} 

其他異常只需要繼承這個類,並相應地改變translationKey。

+0

如果其他異常仍然繼承自System.Exception類,會不會更好? – 2010-01-21 11:59:43

+1

如果TranslatedException從基類Exception繼承(取決於OP未指定的編程語言),則其他異常可以安全地繼承TranslatedException而不會破壞任何(bad-style)catch(Exception ex)代碼。 – dbemerlin 2010-01-21 12:23:23

+0

我可以從資源文件中獲得相應的翻譯。但我只是想知道我是否使用使用錯誤代碼的例外。 – Mohsan 2010-01-21 12:29:30

相關問題