原来的帖子中很多人说了
try... catch .... 得到e.message等 希望不要再贴这个了 谢谢
http://community.csdn.net/Expert/topic/3470/3470657.xml?temp=.3480341
try... catch .... 得到e.message等 希望不要再贴这个了 谢谢
http://community.csdn.net/Expert/topic/3470/3470657.xml?temp=.3480341
解决方案 »
- 内存占用问题
- listBox添加数据和主键值的问题
- 新手求救
- 一用户控件,在他的CODEBEHIND中处理事务,把此用户控件放到一个.NET页面中,这个页面的CODEBEHIND如何获得此用户控件中的对象
- C#中读取数据库为什么会抱错?
- 关于treeview的mousedown问题,高手们请帮忙
- 窗体继承时的问题
- 关于委托的问题!有点逻辑混乱
- 调用remoting服务器端对象的方法时报错:未处理的“System.Runtime.Remoting.RemotingException”类型的异常出现在 mscorlib.dll 中?
- 如果取得数据集中的一个值?
- 广东省高校杯
- C#的抽象类能否定义变量?
关注
例如:
try
{
DateTime dt = DateTime.Paste("1123455");
......
}
catch (FormatException fe)
{
//格式错误处理代码
}
catch (SqlException se)
{
//数据库访问错误处理代码
}
catch (Exception e)
{
//最后默认错误处理代码
}
你可以试一下 jimh(jimmy) 的方法。
以下是一些.Net 可以捕获的异常。
System.ContextMarshalException;
System.AppDomainUnloadedException;
System.ArgumentOutOfRangeException;
System.CannotUnloadAppDomainException;
System.ArrayTypeMismatchException;
System.DivideByZeroException;
System.DllNotFoundException;
System.DuplicateWaitObjectException;
System.EntryPointNotFoundException;
System.Exception;
System.ExecutionEngineException;
System.FieldAccessException;
System.FormatException;
System.IndexOutOfRangeException;
System.InvalidOperationException;
System.InvalidCastException;
System.InvalidProgramException;
System.MemberAccessException;
System.MethodAccessException;
System.MissingFieldException;
System.MissingMemberException;
System.MissingMethodException;
System.MulticastNotSupportedException;
System.NotFiniteNumberException;
System.NotImplementedException;
System.NotSupportedException;
System.NullReferenceException;
System.ObjectDisposedException;
System.OutOfMemoryException;
System.OverflowException;
System.PlatformNotSupportedException;
System.RankException;
System.StackOverflowException;
System.SystemException;
System.TypeInitializationException;
System.TypeLoadException;
System.TypeUnloadedException;
System.UriFormatException;
System.UnauthorizedAccessException;
SqlException是有错误号的。但我想它的错误号应该仅是有关数据库错误的错误号。
每一个异常自然就对应不同的 Err.Number Err.Source等了
扑获了不同的异常,自然也就得到了不同的 Err.Number Err.Source了
无聊就接分来了参考vs.net的例子的处理
///<summary>
///ApplicationAssert 系统调试错误信息声称
///输出系统 Debug 信息到系统日志或抛出异常提示 ,通知调试失败 。
///</summary>
从堆栈调试中取得错误详细信息 ,Debug.Fail通知调试失败, 同时写入系统 Log 。
ApplicationAssert.Check ( 0 > 1 , "Check出错了啊!" , ApplicationAssert.LineNumber ) ;
从堆栈调试中取得错误详细信息 ,Debug.Fail通知调试失败, 不写入系统 Log ,但是抛出异常 ,在 web 上显示错误 。
ApplicationAssert.CheckCondition(booksTable.Rows.Count == 1, "Invalid Book Id Number", ApplicationAssert.LineNumber);
//----------------------------------------------------------------
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
//
// This source code is intended only as a supplement to Microsoft
// Development Tools and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
//namespace Duwamish7.SystemFramework
namespace Paladin.Common
{
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
/// <summary>
/// A class to help with error checking and automatic logging
/// of asserts and conditional checks.
/// <res>
/// This class works with displays system assert dialogs
/// as well as writing to the application log with the
/// ApplicationLog class. There is no instance data associated
/// with this class.
/// </res>
/// </summary>
public class ApplicationAssert
{
#if !DEBUG
/// <value>
/// A LineNumber constant to be used when not in a debug build
/// so that ApplicationAssert. LineNumber is always a valid expression.
/// <res>
/// This allows us to pass ApplicationAssert.LineNumber with good debug
/// functionality and minimal runtime overhead.
/// </res>
/// </value>
public const int LineNumber = 0;
#else
/// <value>Property LineNumber is used to get the current line number in the calling function.</value>
/// <res>
/// This should be called in a parameter list to get accurate
/// information about the line number before the Check* functions
/// are called. If we wait until the Check* functions themselves
/// to retrieve this information, then the stack trace indicates
/// the next executable line, which is only marginally useful
/// information. This function is compiled out in debug builds in
/// favor of the LineNumber constant.
/// Returns LineNumber, or 0 on failure.
/// </res>
public static int LineNumber
{
get
{
try
{
//
// Get the trace information with file information by skipping
// this function and then reading the top stack frame.
//
return (new StackTrace(1, true)).GetFrame(0).GetFileLineNumber();
}
catch
{
} return 0;
}
}
#endif
/// <summary>
/// Check the given condition and show an assert dialog when the
/// desktop is interactive.
/// <res>
/// Log the assertion at a warning level in case the desktop is not
/// interactive. The text will always contain full stack trace
/// information and will show the location of the error condition if
/// the source code is available.
/// </res>
/// <param name="condition">An expression to be tested for True</param>
/// <param name="errorText">The message to display</param>
/// <param name="lineNumber">
/// The line of the current error in the function. See
/// GenerateStackTrace for more information.
/// </param>
/// </summary>
[ConditionalAttribute("DEBUG")]
public static void Check(bool condition, String errorText, int lineNumber)
{
if ( !condition )
{
String detailMessage = String.Empty;
StringBuilder strBuilder;
GenerateStackTrace(lineNumber, out detailMessage);
strBuilder = new StringBuilder();
strBuilder.Append("Assert: ").Append("\r\n").Append(errorText).Append("\r\n").Append(detailMessage);
ApplicationLog.WriteWarning(strBuilder.ToString());
System.Diagnostics.Debug.Fail(errorText, detailMessage);
}
}
/// Verify that a required condition holds.
/// <res>
/// Show an assert dialog in a DEBUG build before throwing an
/// ApplicationException. It is assumed that the exception will be
/// handled or logged, so this does not log a warning for the assertion
/// like the Check function, which does not actually throw.
/// </res>
/// <param name="condition">An expression to be tested for True</param>
/// <param name="errorText">The message to display</param>
/// <param name="lineNumber">
/// The line of the current error in the function. See
/// GenerateStackTrace for more information.
/// </param>
/// <exception class="System.ApplicationException">
/// The checked condition failed.
/// </exception>
/// </summary>
public static void CheckCondition(bool condition, String errorText, int lineNumber)
{
//Test the condition
if ( !condition )
{
//Assert and throw if the condition is not met
String detailMessage;
GenerateStackTrace(lineNumber, out detailMessage);
Debug.Fail(errorText, detailMessage);
throw new ApplicationException(errorText);
}
}
/// <summary>
/// Generate a stack trace to display/log with the assertion text.
/// <res>
/// The trace information includes file and line number information
/// if its available, as well as a copy of the line of text if
/// the source code is available. This function is only included in
/// DEBUG builds of the application.
/// </res>
/// <param name="lineNumber">
/// The line of the current error in the function. This
/// value should be retrieved by call Application.LineNumber
/// in the parameter list of any of the Check* functions. If
/// LineNumber is not provided,then the next executable line is used.
/// </param>
/// <param name="currentTrace">Returns the generated stack trace.</param>
/// </summary>
[ConditionalAttribute("DEBUG")]
private static void GenerateStackTrace(int lineNumber, out String currentTrace)
{
currentTrace = String.Empty;
#if DEBUG
StringBuilder message; //Used for smart string concatenation
String fileName; //The source file name
int currentLine; //The line to process in the source file
String sourceLine; //The line from the source file
StreamReader fileStream = null; //The reader used to scan the source file
bool openedFile = false;
StackTrace curTrace;
StackFrame curFrame;
message = new StringBuilder();
//New StackTrace should never fail, but Try/Catch to be rock solid.
try
{
//Get a new stack trace with line information. Skip the first function
// and second functions (this one, and the calling Check* function)
curTrace = new StackTrace(2, true);
try
{
//
// Get the first retrieved stack frame and attempt to get
// file information from the trace, then open the file
// and find the specified line. Display as much information
// as possible if this is not supported.
//
curFrame = curTrace.GetFrame(0);
//Retrieve and add File/Line information. Note that we only
//proceed if both of these are available.
if ((String.Empty != (fileName = curFrame.GetFileName())) &&
(0 <= (currentLine = (lineNumber != 0) ? lineNumber : curFrame.GetFileLineNumber())))
{
//Append File name and line number
message.Append(fileName).Append(", Line: ").Append(currentLine);
//Append the actual code if we can find the source file
fileStream = new StreamReader(fileName);
openedFile = true;
do
{
sourceLine = fileStream.ReadLine();
--currentLine;
} while (currentLine != 0);
message.Append("\r\n");
if (lineNumber != 0)
{
message.Append("Current executable line:");
}
else
{
message.Append("\r\n").Append("Next executable line:");
}
message.Append("\r\n").Append(sourceLine.Trim());
}
}
catch
{
//Ignore errors, just show as much as we can
}
finally
{
//Always close the file
if (openedFile) fileStream.Close();
}
//Retrieve the final string
currentTrace = message.ToString();
}
catch
{
//Nothing to do, just get out of here with the default (empty) return value
}
#endif
}
} // class ApplicationAssert
} // namespace Duwamish7.SystemFramework
还有你可以参考一下system.Diagnose.Debug等类,也许对你有用。