如何在 类 中显示自定义的错误信息,而不出现编译系统的一大堆错误信息?

wangwm 2005-08-24 10:17:57
如提

例子


try
{
objConn = new SqlConnection(strConnection);
objConn.Open();
}
catch(SqlException ce)
{
throw new Exception("数据库连接错误:"+ce.Message.ToString());
}

这样以后,怎么还会出现如下一大堆的错误信息,我想只出现“数据库连接错误:用户 'sa' 登录失败。 ”这一句,请问高手怎么办啊?



系统错误信息:

数据库连接错误:用户 'sa' 登录失败。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Exception: 数据库连接错误:用户 'sa' 登录失败。

源错误:


行 24: catch(SqlException ce)
行 25: {
行 26: throw new Exception("数据库连接错误:"+ce.Message.ToString());
行 27: }
行 28: }


源文件: c:\inetpub\wwwroot\rong2soft\actiondll\cactiondb.cs 行: 26

堆栈跟踪:


[Exception: 数据库连接错误:用户 'sa' 登录失败。]
Rong2Soft.ActionDLL.cActionDB..ctor() in c:\inetpub\wwwroot\rong2soft\actiondll\cactiondb.cs:26
Rong2Soft.WebForm1..ctor() in c:\inetpub\wwwroot\rong2soft\default.aspx.cs:33
ASP.Default_aspx..ctor() in c:\WINNT\Microsoft.NET\Framework\v1.1.4322\Temporary ASP.NET Files\rong2soft\c00e56c3\c41d6367\zvrgrath.0.cs:0

[TargetInvocationException: 调用的目标发生了异常。]
System.RuntimeType.CreateInstanceImpl(Boolean publicOnly) +0
System.Activator.CreateInstance(Type type, Boolean nonPublic) +66
System.Activator.CreateInstance(Type type) +7
System.Web.HttpRuntime.CreatePublicInstance(Type type)
System.Web.UI.TemplateControlParser.GetCompiledInstance(String virtualPath, String inputFile, HttpContext context)

[HttpException (0x80004005): 未能创建类型为“ASP.Default_aspx”的页。]
System.Web.UI.TemplateControlParser.GetCompiledInstance(String virtualPath, String inputFile, HttpContext context)
System.Web.UI.PageParser.GetCompiledPageInstanceInternal(String virtualPath, String inputFile, HttpContext context)
System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String path)
System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, String path, String pathTranslated, Boolean useAppConfig)
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication+IExecutionStep.Execute()
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)


...全文
160 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChengKing 2005-08-24
  • 打赏
  • 举报
回复
throw new Exception("数据库连接错误:"+ce.Message.ToString());

改为:

throw new Exception("数据库连接错误:");

试一下
wangwm 2005-08-24
  • 打赏
  • 举报
回复
按照CuiQingShaShou(摧情杀手) 的做法:

错误信息如下:

未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误:


行 24: catch(SqlException ce)
行 25: {
行 26: withpage.Response.Write("数据库连接错误"+ce.Message.ToString());
行 27: }
行 28: }

CuiQingShaShou 2005-08-24
  • 打赏
  • 举报
回复
public class WithErr
{
System.Web.UI.Page withpage;
public WithErr(System.Web.UI.Page withPage)
{
withpage=withPage;
}
public void OpenData()
{
try
{
objConn = new SqlConnection(strConnection);
objConn.Open();
}
catch(SqlException ce)
{
withpage.Response.Write("<script language ='javascript'>alert('数据库连接错误:"+ce.Message.ToString+"')</script>");
//throw new Exception("数据库连接错误:"+ce.Message.ToString());
}
}
}
调用:
WithErr handerErr=new WithErr(this);
handerErr.OpenData();



也可以申明为静态的方法:

public class WithErr
{
public WithErr()
{
}
public static void OpenData(System.Web.UI.Page withPage)
{
try
{
objConn = new SqlConnection(strConnection);
objConn.Open();
}
catch(SqlException ce)
{
withPage.Response.Write("<script language ='javascript'>alert('数据库连接错误:"+ce.Message.ToString+"')</script>");
//throw new Exception("数据库连接错误:"+ce.Message.ToString());
}
}
}
调用:
WithErr.OpenData(this);
baby21st 2005-08-24
  • 打赏
  • 举报
回复
把这个去掉 ce.Message.ToString() 不就没有一大堆了嘛
athossmth 2005-08-24
  • 打赏
  • 举报
回复
使用HttpResponse
love_AC 2005-08-24
  • 打赏
  • 举报
回复
怎么不能呢》?
wangwm 2005-08-24
  • 打赏
  • 举报
回复
to powerllr(笨笨的招财鸡)


类里面不能使用Response.Write()啊
wangwm 2005-08-24
  • 打赏
  • 举报
回复


类里面不能使用Response.Redirect()

怎么传到错误页面啊?
powerllr 2005-08-24
  • 打赏
  • 举报
回复
try
{
objConn = new SqlConnection(strConnection);
objConn.Open();
}
catch(SqlException ce)
{
Response.Write("数据库连接错误:"+ ce.Message);
}
这个Message就是简化了的错误
lovefootball 2005-08-24
  • 打赏
  • 举报
回复
你可以把错误放到一个页面

然后传递各种不同的参数进去,显示不同的错误
CuiQingShaShou 2005-08-24
  • 打赏
  • 举报
回复
public class WithErr
{
System.Web.UI.Page withpage;
public WithErr(System.Web.UI.Page withPage)
{
withpage=withPage;
}
public void OpenData()
{
try
{
objConn = new SqlConnection(strConnection);
objConn.Open();
}
catch(SqlException ce)
{
withpage.Response.Write("<script language ='javascript'>alert('数据库连接错误:"+ce.Message.ToString+"')</script>");
//throw new Exception("数据库连接错误:"+ce.Message.ToString());
}
}
}
调用:
WithErr handerErr=new WithErr(this);
handerErr.OpenData();



也可以申明为静态的方法:

public class WithErr
{
public WithErr()
{
}
public static void OpenData(System.Web.UI.Page withPage)
{
try
{
objConn = new SqlConnection(strConnection);
objConn.Open();
}
catch(SqlException ce)
{
withPage.Response.Write("<script language ='javascript'>alert('数据库连接错误:"+ce.Message.ToString+"')</script>");
//throw new Exception("数据库连接错误:"+ce.Message.ToString());
}
}
}
调用:
WithErr.OpenData(this);

记得调用的时候参数一定是this(当前页面)
如不行就去掉 ce.Message.ToString() 改为:

withPage.Response.Write("<script language ='javascript'>alert('数据库连接错误:"')</script>");

试试看
wangwm 2005-08-24
  • 打赏
  • 举报
回复
按照 ChengKing(bantamweight) 的方法
错误如下:

数据库连接错误:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Exception: 数据库连接错误:

源错误:


行 24: catch(SqlException ce)
行 25: {
行 26: throw new Exception("数据库连接错误:");
行 27: }
行 28: }


62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧