2012-02-20 5 views
1

로깅을 지정하여이를 catch하기 위해 새로운 예외 유형을 생성하려고합니다. 하지만 int 값을 ctor에 전달하려고합니다. 그것을하는 방법? 나는 시도했다 :새로운 예외 유형 생성

public class IncorrectTimeIdException : Exception 
{ 
    public IncorrectTimeIdException(int TypeID) : base(message) 
    { 
    } 
} 

나는 편집하는 동안 오류가 발생했다.

+5

'message'가 정의되어 있지 않습니다. 따라서 base (메시지) 대신 base ("Something wrong") – mshsayem

+4

예외 메시지 (의도적 인 말투 없음)를 사용하면이 경우에 무엇이 잘못되었는지 정확히 알려야합니다. – BrokenGlass

+2

http://blog.gurock.com/articles/creating- dotnet에서 사용자 정의 예외 - 새로운 예외 유형에 대한 사용법 – ken2k

답변

2
public class IncorrectTimeIdException : Exception 
{ 
    private void DemonstrateException() 
    { 
     // The Exception class has three constructors: 
     var ex1 = new Exception(); 
     var ex2 = new Exception("Some string"); // <-- 
     var ex3 = new Exception("Some string and InnerException", new Exception()); 

     // You're using the constructor with the string parameter, hence you must give it a string. 
    } 

    public IncorrectTimeIdException(int TypeID) : base("Something wrong") 
    { 
    } 
} 
+0

빠른 질문. 예외를 생성 할 때 DemonstrateException과 유사한 메소드를 호출합니까? 아니면 자연스럽게 완료 되었습니까? –

1

이 메시지는 - message이 정의되지 않았 음을 정확히 알려줍니다. 다른 방법이

public IncorrectTimeIdException(string message, int TypeID) : base(message) 
{ 
} 

// Usage: 
throw new IncorrectTimeIdException("The time ID is incorrect", id); 

또는 어떤 메시지와 함께 예외가 생성 :

public IncorrectTimeIdException(int TypeID) 
{ 
} 

또는 마지막으로이

당신은 예외를 만들 때 사용자 메시지를 제공 할 수 있도록하는 대신이 시도 , 미리 정의 된 메시지와 함께 예외를 만듭니다.

public IncorrectTimeIdException(int TypeID) : base("The time ID is incorrect") 
{ 
} 

If 클래스에 여러 생성자를 선언 할 수도 있기 때문에 미리 정의 된 메시지를 사용하는 생성자를 제공 할 수 있으며 동시에 해당 메시지를 재정의 할 수있는 생성자를 제공 할 수 있습니다.

2

여기에 몇 가지 추가 데이터 (유형 ID에서)를 전달하는 맞춤 예외 클래스를 만드는 데 사용할 수있는 코드와 맞춤 예외를 만들기위한 모든 '규칙'이 있습니다. 예외 클래스 및 설명이 거의없는 사용자 지정 데이터 필드의 이름을 원하는대로 바꿀 수 있습니다.

using System; 
using System.Runtime.Serialization; 

[Serializable] 
public class CustomException : Exception { 

    readonly Int32 data; 

    public CustomException() { } 

    public CustomException(Int32 data) : base(FormatMessage(data)) { 
    this.data = data; 
    } 

    public CustomException(String message) : base(message) { } 

    public CustomException(Int32 data, Exception inner) 
    : base(FormatMessage(data), inner) { 
    this.data = data; 
    } 

    public CustomException(String message, Exception inner) : base(message, inner) { } 

    protected CustomException(SerializationInfo info, StreamingContext context) 
    : base(info, context) { 
    if (info == null) 
     throw new ArgumentNullException("info"); 
    this.data = info.GetInt32("data"); 
    } 

    public override void GetObjectData(SerializationInfo info, 
    StreamingContext context) { 
    if (info == null) 
     throw new ArgumentNullException("info"); 
    info.AddValue("data", this.data); 
    base.GetObjectData(info, context); 
    } 

    public Int32 Data { get { return this.data; } } 

    static String FormatMessage(Int32 data) { 
    return String.Format("Custom exception with data {0}.", data); 
    } 

}