我有一个调用SOAP接口的类,并获取一个数据数组.但是,如果此请求超时,则会引发异常.这很好.但是,我希望我的程序再次尝试进行此调用.如果它超时,我希望继续拨打这个电话,直到它成功为止.我怎么能做到这一点?
例如:
try
{
salesOrdersArray = MagServ.salesOrderList(sessID, filter);
}
catch
{
?? What Goes Here to FORCE the above line of code to rerun until it succeeds.
}
Run Code Online (Sandbox Code Playgroud) 我是Selenium webdriver的新手,也许这个问题很明显.我是这样的情况:
如果该元素存在,请单击它并返回索引页面:
driver.findElement(By.id("...."])).click();
Run Code Online (Sandbox Code Playgroud)
如果不退出,请跳过它并返回索引页面.测试仍然没有任何异常抛出.
我知道一个解决方案:
driver.findElements( By.id("...") ).size() != 0
Run Code Online (Sandbox Code Playgroud)
所以我试过了:
if(driver.findElements(By.id("....")).size() > 0)
{
driver.findElement(By.id("....")).click();
driver.findElement(By.cssSelector("...")).click();
}
else
{
driver.findElement(By.cssSelector("....")).click();
}
Run Code Online (Sandbox Code Playgroud)
事实证明这很难看,因为如果我有10个要验证的元素,那么这个IF条件需要写10次.
任何解决方法,使它整洁?
我已经尝试了一些关于如何在抛出异常时在堆栈跟踪中保留正确行号的建议。最常见的就是接住然后扔掉,但这不起作用。以下是我尝试过的一些:
private void button1_Click(object sender, EventArgs e)
{
try
{
test();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void test()
{
try
{
int a = 1;
int b = 0;
int x = 0;
x = a / b;
}
catch
{
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
还有 catch 块的一些变化。
catch (Exception)
{
throw;
}
catch (Exception e)
{
throw;
}
catch (Exception e)
{
throw e;
}
Run Code Online (Sandbox Code Playgroud)
所有这些都在抛出行和消息框行上报告错误 - 永远不会在除以 0 的行上报告错误。如果我在 test() 函数内部中断,它确实会显示正确的行 #,但在抛出后不会显示错误。唯一有效的方法是在 test() 函数中不要有任何 try/catch。但当然我希望能够捕获错误并重新抛出它们并保持堆栈跟踪正确。那么这是如何做到的呢? …
如果我有这样的代码:
void a()
{
try
{
b();
}
catch (MyException)
{
// Handle any problems that occurred in b(c(d()))
}
}
void b()
{
c();
// Do something else
}
void c()
{
d();
// Do something else
}
void d()
{
// Do something, throw a MyException if it fails
}
Run Code Online (Sandbox Code Playgroud)
假设在任何时候都不需要清理,最好在c()中调用d()和在b()中调用c()时调用try {} catch {throw;},还是认为OK让d()的异常在没有任何介入的try/catch块的情况下自然地冒泡到a()"自然"?
我认为额外的try/catch块可以作为一种"文档",但它们似乎是多余的,所以我只是想知道其他人会认为最好的方法.
对不起,如果这有点太基础了,我试图了解异常,但我似乎对它们没有好感觉.
嗨,我想知道你对异常处理的看法是什么,即我有一个方法:
public void Method{}
{
for (int i=0;i < length )
{
// dosomething that may case exception
...
...
// rest of the code
}
}
Run Code Online (Sandbox Code Playgroud)
我应该为整个循环添加try catch块以进行异常处理,还是只添加最有价值的代码或其他东西?什么是最佳做法?
我有两个例子.在第一种情况下,调试器捕获未处理的异常:
static void Main(string[] args) {
Exec();
}
static void Exec() {
throw new Exception();
}
Run Code Online (Sandbox Code Playgroud)
并且异常具有完整的堆栈跟踪:
at ConsoleApplication28.Program.Exec()
at ConsoleApplication28.Program.Main(String[] args)
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
Run Code Online (Sandbox Code Playgroud)
第二种情况:
static void Main(string[] args) {
Exec();
}
static void Exec() {
try { …Run Code Online (Sandbox Code Playgroud) 考虑一个示例,其中用户使用两个 aspx 页面中的表单输入客户对象的值。使用第一种方法,在调用构造函数之前,两个 aspx 页面都需要验证 ID 大于 0 并且 FirstName 不为空。使用第二个选项,两个页面都可以调用验证函数并向用户显示错误消息。
根据上面的例子,我更喜欢第二种选择。然而,当我在网上研究时,我不断发现它更面向对象,立即抛出异常而不让对象接受无效数据。正如我之前所说,除了例外情况,调用此构造函数的每个页面都需要验证输入是否有效。我不喜欢重复这个逻辑,所以我更喜欢第二种选择。
领域驱动设计方面的首选选项是什么?
选项1
public class Customer{
public int ID { get; set; }
public string FirstName { get; set; }
public Customer(int ID, string FirstName){
if (ID < 0)
throw new Exception("ID cannot be less than 0");
if (string.IsNullOrEmpty(FirstName))
throw new Exception("First Name cannot be empty");
this.ID = ID;
this.FirstName = FirstName;
}
}
Run Code Online (Sandbox Code Playgroud)
选项2
public class Customer{
public int ID { get; set; }
public string FirstName { …Run Code Online (Sandbox Code Playgroud) 在throw;和之间选择的最佳做法是throw ex;什么?有没有?关于 - 例如 - 这个简单的片段:
try{
// some code
} catch (Exception ex) {
// some catcher code
// throw; ?
// or
// throw ex; ?
// how to decide which one?
}
Run Code Online (Sandbox Code Playgroud)
更新: 我知道上面的两个区别.问题是如何决定使用其中之一?有没有最好的做法来做出更好的选择?
我目前有一个数据访问层,它也使用Web服务公开一些API.
[WebMethod]
public List<GlobalStat> GetStats()
{
List<GlobalStat> Stats = new List<GlobalStat>();
string sql = @"
A huge multi-line SQL query
";
try
{
string ConString = Constants.connString;
con = new SqlConnection(ConString);
cmd = new SqlCommand(sql, con);
con.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
GlobalStat stat = new GlobalStat();
stat.Key = dr[0].ToString();
stat.Value = int.Parse(dr[1].ToString());
Stats.Add(stat);
}
}
catch (Exception x)
{
Response.Write(x);
}
return Stats;
}
Run Code Online (Sandbox Code Playgroud)
我有点担心SQL的编写方式.
有很多东西硬编码到这里:数据库名称,表名等.
要解决这个问题,我是否只是在一个地方创建一个包含所有SQL命令的单独的全局文件,或者是否有更好的范例?我不是在应用程序内部创建SQL表,但这些表驻留在不同的预构建数据库中.
我应该如何构建一个使用内联SQL从数据库生成数据的应用程序?
怎么
throw
{
name: 'type error',
message: 'provide numeric value'
};
Run Code Online (Sandbox Code Playgroud)
语法不正确的时候
throw{
name: 'type error',
message: 'provide numeric value'
};
Run Code Online (Sandbox Code Playgroud)
语法是否正确?
是否真的有必要附加花括号和为什么?