C #에서 사용자 지정 예외를 구현하기위한 업계 표준 모범 사례는 무엇입니까?
C #에서 사용자 지정 예외를 구현하기위한 업계 표준 모범 사례는 무엇입니까?
Google을 확인한 결과 많은 추천이 있지만 어느 것이 더 신뢰할 수 있는지 모르겠습니다.
누구든지 권위있는 기사에 대한 링크가 있으면 도움이 될 것입니다.
사용자 지정 예외를 만드는 표준은 Exception 에서 파생하는 것 입니다. 그런 다음 고유 한 속성 / 메서드 및 오버로드 된 생성자를 도입 할 수 있습니다 (해당하는 경우).
다음은 ConnectionFailedException
예외 유형에 특정한 추가 매개 변수를 받는 사용자 정의의 기본 예입니다 .
[Serializable]
public class ConnectionFailedException : Exception
{
public ConnectionFailedException(string message, string connectionString)
: base(message)
{
ConnectionString = connectionString;
}
public string ConnectionString { get; private set; }
}
응용 프로그램에서 이것은 응용 프로그램이 데이터베이스에 연결을 시도하는 시나리오에서 사용될 수 있습니다.
try
{
ConnectToDb(AConnString);
}
catch (Exception ex)
{
throw new ConnectionFailedException(ex.Message, AConnString);
}
ConnectionFailedException
더 높은 수준에서 처리하는 것은 귀하에게 달려 있습니다 (해당하는 경우).
사용자 지정 예외 및 사용자 지정 예외 디자인도 살펴보십시오.
다음은 사용자 지정 예외를 만드는 코드입니다.
using System;
using System.Runtime.Serialization;
namespace YourNamespaceHere
{
[Serializable()]
public class YourCustomException : Exception, ISerializable
{
public YourCustomException() : base() { }
public YourCustomException(string message) : base(message) { }
public YourCustomException(string message, System.Exception inner) : base(message, inner) { }
public YourCustomException(SerializationInfo info, StreamingContext context) : base(info, context) { }
}
}
참조 : http://www.capprime.com/software_development_weblog/2005/06/16/CreatingACustomExceptionClassInC.aspx
나는 당신이 예외 처리 방법을 찾고 있다고 가정합니다. 따라서 다음 기사를 살펴보십시오.
http://msdn.microsoft.com/en-us/library/ms229014.aspx // 사용자 지정 예외를 포함하여 예외에 대한 전반적인 아이디어를 제공합니다.
사용자 지정 예외를 사용하여 오류의 특성을 전달합니다.
For example, I like using the framework provided "ArgumentNullException" to check arguments. Then later, when I see this error either in the debugger, or in an error log, I immediately know the nature of the error without reading any further.
The other end of the spectrum is the InvalidOperationException which could mean pretty much anything.
The alternative to custom exceptions is detailed error messages. That's ok, but by making a custom exception such as ConnectionFailed is more meaningful. Then the message itself can give greater details.
When creating such custom exceptions, I do not add any new properties. The reason for this is that if you have an error logger, you want it to work on all exceptions. If you add a special property, then the error logger is going to ignore it. For example, if you use MSTest, when you run your test and it fails, the custom properties are not displayed. But if you stick with the Message property of the baseclass, it will display just fine.
So the subclassing is very simple:
public class NavigationException : Exception{
public NavigationException() {}
public NavigationException(string msg) : base(msg) {}
public NavigationException(string msg, Exception inner) : base(msg, inner) {}
}
This is very straightforward, works with any error logger, and when I see it, I know it was a Navigation problem and I can view the details if needed.
Greg
'Program Tip' 카테고리의 다른 글
Pycharm에서 문자열의 맞춤법 검사를 피하는 방법 (0) | 2020.11.18 |
---|---|
SQLite, Python, 유니 코드 및 비 UTF 데이터 (0) | 2020.11.18 |
jQuery 로딩을 연기 할 수 있습니까? (0) | 2020.11.18 |
Java7“Solr / Lucene”버그는 얼마나 심각한가요? (0) | 2020.11.18 |
파일 간 SQLAlchemy 클래스 (0) | 2020.11.18 |