【发布时间】:2015-07-03 18:14:21
【问题描述】:
我试图捕捉一个抛出的异常,但它不会冒泡到它被调用的地方。它打破了InsertNewUser 的catch 块,说
“PeakPOS.exe 中出现“System.Exception”类型的异常,但未在用户代码中处理”
如果我单击调试器继续,它会转到某个名为App.g.i.cs 的文件并在我不理解的行上中断,但与中断时的调试有关。之后应用程序终止。
为什么在重新抛出异常然后重新捕获并处理(待处理)时说未处理?
AccessViewModel.cs
public void SaveNewUser(Popup popup)
{
UserAccounts.Add(TempUser);
string salt = PeakCrypto.GenerateSalt();
string hash = PeakCrypto.GenerateHashedPassword(Password + salt);
try
{
PeakDB.InsertNewUser(TempUser, salt, hash);
}
catch (Exception e)
{
//TODO notify user that new account could not be saved
}
CreateNewAccount();
if (popup != null)
popup.IsOpen = false;
}
PeakDB.cs
public static async void InsertNewUser(UserAccount user, String salt, String hash)
{
var db = await DatabaseHelper.GetDatabaseAsync();
try
{
using (var userStatement = await db.PrepareStatementAsync(
"INSERT INTO AccessAccounts (FirstName, LastName, Salt, Hash) VALUES(@first, @last, @salt, @hash)"))
{
userStatement.BindTextParameterWithName("@first", user.FirstName);
userStatement.BindTextParameterWithName("@last", user.LastName);
userStatement.BindTextParameterWithName("@salt", salt);
userStatement.BindTextParameterWithName("@hash", hash);
await userStatement.StepAsync();
}
}
catch(Exception e)
{
// TODO: log the exception error
throw;
}
}
App.g.i.cs
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
【问题讨论】:
-
您是否尝试取消选中
Break when this Exception occurs复选框并再次运行?我认为在调试模式下它确实会在异常时中断 -
@Saagar:是的。发生的事情是它进入 App.g.i.cs 文件并像以前一样在 if 语句上中断,它只是不再在 catch 中中断。我想知道我是否误解了try/catch。是不是捕获了异常而不处理它?
标签: c# exception-handling try-catch