捕获异常并重新抛出异常时需要考虑哪些最佳实践?我想确保保留Exception对象InnerException和堆栈跟踪.以下代码块在处理此方式时是否存在差异?
try
{
//some code
}
catch (Exception ex)
{
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
VS:
try
{
//some code
}
catch
{
throw;
}
Run Code Online (Sandbox Code Playgroud) 如果方法名称以"Try"开头,我们正在与我们的同事讨论它意味着什么.
有以下意见:
什么是官方定义?"尝试"在方法名称中说什么?有关于此的官方指南吗?
好吧,这很简单:
Application.ThreadException和之间有什么区别AppDomain.CurrentDomain.UnhandledException?
我需要处理两者吗?
谢谢!
我已经"继承"了一个C#方法,该方法创建了一个ADO.NET SqlCommand对象,并循环遍历要保存到数据库的项目列表(SQL Server 2005).
现在,使用传统的SqlConnection/SqlCommand方法,为了确保一切正常,两个步骤(删除旧条目,然后插入新条目)被包装到ADO.NET SqlTransaction中.
using (SqlConnection _con = new SqlConnection(_connectionString))
{
using (SqlTransaction _tran = _con.BeginTransaction())
{
try
{
SqlCommand _deleteOld = new SqlCommand(......., _con);
_deleteOld.Transaction = _tran;
_deleteOld.Parameters.AddWithValue("@ID", 5);
_con.Open();
_deleteOld.ExecuteNonQuery();
SqlCommand _insertCmd = new SqlCommand(......, _con);
_insertCmd.Transaction = _tran;
// add parameters to _insertCmd
foreach (Item item in listOfItem)
{
_insertCmd.ExecuteNonQuery();
}
_tran.Commit();
_con.Close();
}
catch (Exception ex)
{
// log exception
_tran.Rollback();
throw;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我最近一直在阅读很多关于.NET TransactionScope类的内容,我想知道,这里首选的方法是什么?通过切换使用,我可以获得任何东西(可读性,速度,可靠性)
using (TransactionScope _scope = new TransactionScope())
{ …Run Code Online (Sandbox Code Playgroud) 我有一些目前看起来像这样的代码:
public void MainFunction()
{
try
{
SomeProblemFunction();
}
catch
{
AllFineFunction();
}
}
private void SomeProblemFunction() { ... }
private void AllFineFunction() { ... }
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我正在将调用包装SomeProblemFunction在一个try语句周围,因为该函数可能会失败(它依赖于外部Web服务调用).
我的问题是:try声明应该是a)问题函数之外(比如我现在有)或b)问题函数内部?
谢谢.
在Javascript中,您可以delete使用对象属性:
var o = { x: 1, y: 2 };
var wasDeleted = delete o.x;
Run Code Online (Sandbox Code Playgroud)
现在o.x应该是undefined,现在wasDeleted是true。
但是,您只能删除本机对象,不幸的是,浏览器对此似乎有不同的想法:
window.x = 1;
delete window.x;
Run Code Online (Sandbox Code Playgroud)
现在在Chrome和IE9-10 x中将是undefined,但在IE6-8中将引发异常:
“对象不支持此操作”
大。请注意,这并非delete不受支持...
// Oops, no var, so this is now a global, should've 'use strict'
o = { x: 1, y: 2 };
// Works
delete o.x;
// Works
delete window.o.y;
// Fails, but only in IE6-8 :-(
delete window.o
Run Code Online (Sandbox Code Playgroud)
我意识到我可以添加一个 …
javascript cross-browser ecmascript-5 browser-feature-detection
我试图避免在捕获时返回不正确的值,但我找不到比这更好的解决方案:
private SecurityLevel ApiGetSecurityLevel()
{
try
{
return _BioidInstance.GetSecurityLevel();
}
catch
{
return SecurityLevel.High;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法这样做,所以我不返回不正确的值?我无法更改SecurityLevel枚举.
在我的应用程序中,我想在遇到崩溃的情况下将日志发送到远程服务器.我已经添加了try-catch块并且在catch中我将日志发送到服务器.我想知道我应该抓住的所有例外情况.每次崩溃都需要日志,以便我可以修复它.赶上所有异常会是一个好习惯吗?
提前致谢.
在执行某些操作后,将异常从周围方面重新抛出到ExceptionHandlerrest控制器中是否正常,如下所示:
@Around("execution(* *(..)) && @annotation(someAnnotation)")
public Object catchMethod(ProceedingJoinPoint point, SomeAnnotation someAnnotation) throws Throwable {
//some action
try {
result = point.proceed();
} catch (Exception e) {
//some action
throw e; //can I do this?
}
//some action
return result;
}
Run Code Online (Sandbox Code Playgroud)
它正在工作,但我不知道也许出于某种原因我没有这样做。
我想知道ApplicationException当用户违反某些业务规则时,是否建议使用它来返回应用程序错误.例如:
public void validate(string name, string email)
{
int count1 = (from p in context.clients
where (p.name == clients.name)
select p).Count();
if (count1 > 0)
throw new ApplicationException("Your name already exist in the database");
int count2 = (from p in context.clients
where (p.email == clients.email)
select p).Count();
if (count2 > 0)
throw new ApplicationException("Your e-mail already exist in the database");
}
Run Code Online (Sandbox Code Playgroud)
这是一个好的或坏的策略?如果不是,那会是更好的方法?
我的preveus问题中的这个主题:如何在Windows事件查看器中删除和创建日志
我创建了wpf app.我用3种方式捕捉未处理的异常:
public partial class App : Application
{
public App()
{
DispatcherUnhandledException += App_DispatcherUnhandledException;
Dispatcher.UnhandledException += Dispatcher_UnhandledException;
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
}
private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
}
private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我正在创建这样的异常:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
throw new Exception("Test exception");
}
}
Run Code Online (Sandbox Code Playgroud)
在执行方法(Dispatcher_UnhandledException,)之后CurrentDomain_UnhandledException,App_DispatcherUnhandledException这个异常仍在抛出.Windows事件查看器正在创建这样的日志
描述:由于未处理的异常,进程终止.异常信息:System.Data.ProviderBase.DbConnectionFactory.TryGetConnection的System.InvalidOperationException(System.Data.Common.DbConnection,System.Threading.Tasks.TaskCompletionSource
1<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal, System.Data.ProviderBase.DbConnectionInternal ByRef) …
在Visual Studio 2010 C#项目中,如何在没有try-catch块的情况下编写是否编写了任何方法?
有时作为代码审阅者,很难搜索根据代码标准没有正确编写的函数/方法,因为没有try-catch块.
我知道Visual Studio 中的" 查找"选项支持正则表达式.但是,能够巧妙地完成这项工作的正则表达式是什么?
c# ×7
.net ×3
java ×2
ado.net ×1
android ×1
aop ×1
asp.net ×1
asp.net-mvc ×1
ecmascript-5 ×1
exception ×1
javascript ×1
regex ×1
rethrow ×1
spring ×1
spring-aop ×1
sql-server ×1
transactions ×1
winforms ×1
wpf ×1