各位,问题如上述所示,示例代码如下:
public virtual sbyte[] decodeBytes(QRCodeImage qrCodeImage)
{
if (results.Count == 0)
throw new DecodingFailedException("Give up decoding");
}下面是自定义的一个异常:
using System;
namespace ThoughtWorks.QRCode.ExceptionHandler
{
[Serializable]
public class DecodingFailedException : System.ArgumentException
{
internal String message = null; public override String Message
{
get
{
return message;
}
}
public DecodingFailedException(String message)
{
this.message = message;
}
}
}本人对异常实在有些雾水,还请各位指点一二,不胜感激。
public virtual sbyte[] decodeBytes(QRCodeImage qrCodeImage)
{
if (results.Count == 0)
throw new DecodingFailedException("Give up decoding");
}下面是自定义的一个异常:
using System;
namespace ThoughtWorks.QRCode.ExceptionHandler
{
[Serializable]
public class DecodingFailedException : System.ArgumentException
{
internal String message = null; public override String Message
{
get
{
return message;
}
}
public DecodingFailedException(String message)
{
this.message = message;
}
}
}本人对异常实在有些雾水,还请各位指点一二,不胜感激。
try{
decodeBytes()
}
catch(DecodingFailedException e){
log(...)
}
try
{
// ......
}
catch (Exception ex) { textBox1.Text = ex.ToString(); }
也可以用一些log工具 比如 log4net就比较简单适用 功能也比较多
参考
private int stringToInt(string source)
{
int result = 0;
if (!Int32.TryParse(source, out result))
{
throw new LoginException(source);//这里抛出异常
} return result;
}
try
{
stringToInt("dfdf");
}
catch (LoginException ex)//这里捕捉异常
{
//处理异常 Response.Write(ex.Title); }
{
int result = 0;
if (!Int32.TryParse(source, out result))
{
throw new LoginException(source);//方法里面抛出异常
} return result;
} protected void Page_Load(object sender, EventArgs e)
{ try
{
stringToInt("dfdf");
}
catch (LoginException ex)
{
//处理异常
Response.Write(ex.Title); }
}
try
{
// ...
}
catch (Exception ex)
{
// 写到文件中
StreamWriter sw = File.AppendText(@"c:\err.txt");
sw.WriteLine(ex.ToString());
sw.Flush();
sw.Close();
}
那你就不要try、catch,直接要它报错,终止程序的运行。
你要处理异常,又要程序停止运行,呵呵,这个倒是第一次碰到,等待高手。
{
// ...
}
catch (Exception ex)
{
// 写到文件中
StreamWriter sw = File.AppendText(@"c:\err.txt");
sw.WriteLine(ex.ToString());
sw.Flush();
sw.Close();
}这个写的对