对不起,如果我的标题有点混乱。
我的程序正在执行一些网络抓取,因此会由于随机网络条件而捕获一些 SocketTimeoutException。现在,当 SocketTimeoutException 被捕获时,该特定循环将被跳过,因此我会错过一些数据。我确信当跳过循环中的代码再次运行时,一切都会好起来的。由于我正在抓取大量数据(> 100 万组数字),我不想记录异常循环并再次手动运行它们。捕获异常时有没有办法再次运行相同的循环?
try{
for(){
someCode
...
}
}catch(IOException){
}
Run Code Online (Sandbox Code Playgroud) 我是 php 新手,所以这听起来可能很简单 这是我的 php 代码
<?php
try
{
echo "Hello";
}
catch (Exception $e)
{
echo $e;
}
?>
Run Code Online (Sandbox Code Playgroud)
这输出Hello
现在我修改了代码以获得异常
<?php
try
{
ech "Hello";
}
catch (Exception $e)
{
echo $e;
}
?>
Run Code Online (Sandbox Code Playgroud)
但它什么也没打印。catch 不是应该打印错误吗?或者我做错了什么
我如何跳转到 try catch 中的 catch 块:
Run Code Online (Sandbox Code Playgroud)try{ if(bla!=) throws Exception(); }catch(exception){ //do }
不起作用?
如果我运行下面调用 Test1() 的代码,VS2012 将在该行很好地中断并准确显示发生了什么。如果我注释掉 Test1 并放入 Test2,那么 try catch 不会停止在线,它只是将其注销到控制台。
我怎样才能让 VS2012 停止在错误行上,即使它被 try catch 语句包围?
private void Button_Click(object sender, RoutedEventArgs e)
{
//Test1(); // No try catch - stops on error line dividing by zero.
Test2(); // Try catch - Just writes out to console.
}
private void MakeError()
{
int i = -1;
i += 1;
int j = 1 / i;
Console.WriteLine(j.ToString());
}
void Test1()
{
Console.WriteLine("MakeError in Test1");
MakeError();
}
void Test2()
{
Console.WriteLine("MakeError in Test2"); …
Run Code Online (Sandbox Code Playgroud) 我一直在创建一个对文件进行排序的程序,我需要尝试捕捉错误
这是代码:
try:
shutil.move(path, directory)
except OSError:
print 'ERROR: File name already in use'
quit()
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这个错误时:
except OSError:
^
IndentationError: unexpected unindent
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激
下面的代码生成一个 Computed property must have an explicit type error.
使用新的try/catch
Swift 语法列出文件的正确方法是什么?
do {
let files = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(docsPath as String) as! [String] {
print("Found \(files.count) file(s) in \(docsPath):")
}catch{
print("Error with listing files: \(error)")
}
Run Code Online (Sandbox Code Playgroud) 当您在数据库中没有要删除的内容时,我尝试显示一条消息,而不是显示一个错误,表明您有一个空值
public function destroy($customer_id)
{
$customer_response = [];
$errormsg = "";
$customer = Customer::find($customer_id);
$result = $customer->delete();
try{
//retrieve page
if ($result){
$customer_response['result'] = true;
$customer_response['message'] = "Customer Successfully Deleted!";
}else{
$customer_response['result'] = false;
$customer_response['message'] = "Customer was not Deleted, Try Again!";
}
return json_encode($customer_response, JSON_PRETTY_PRINT);
}catch(\Exception $exception){
dd($exception);
$errormsg = 'No Customer to de!' . $exception->getCode();
}
return Response::json(['errormsg'=>$errormsg]);
}
Run Code Online (Sandbox Code Playgroud)
与我之前的商店功能相比,try/catch 方法不起作用
到目前为止,这是我的代码:
$Folders = Get-ChildItem -Path U:\Powershell\Move-Files\Clients |
?{$_.PsIsContainer} |
Select -ExpandProperty FullName
Get-ChildItem -Path U:\Powershell\Move-Files\Individual | ?{
!($_.PsIsContainer)
} | %{
#Part of the file to match
$File = ($_.name.substring(0,$_.basename.length-11))
$File = ($File -split '_')[1] + ", " + ($File -split '_')[0]
# Find matching Directory
$Path = $Folders | ?{$_ -match $File}
Move-Item -Path $_.FullName -Destination $Path -ErrorAction Silently Continue
Write-Host "Moved $_.FullName to $Path"
}
Run Code Online (Sandbox Code Playgroud)
基本上,我有很多命名的文件First_Last (MMM yyyy).pdf
,这会获取每个文件并以 的格式创建一个变量,Last, First
以便它可以进行部分匹配并将文件移动到目标文件夹(格式为Last, First …
我有以下代码:
try
{
Data.CreateUserAndAccount.ExecuteProcedure(accountName, firstName, lastName, email, password);
return String.Empty;
}
catch (SqlException databaseError)
{
string result = IdentifyError(databaseError);
return result;
}
catch (Exception)
{
throw;
}
Run Code Online (Sandbox Code Playgroud)
我通过违反唯一键故意抛出 SQL 异常。但是,异常被Exception
而非捕获SqlException
。当我检查InnerException
详细信息时,异常类型为System.Data.SqlClient.SqlException
。
为什么 SQL 异常只能被泛型捕获Exception
?
我目前正在开发一个评分应用程序。当我编写的方法运行成功时,我想通知用户操作成功。但是,当我尝试通过将方法放在 try/catch/finally 块中来实现此目的时,无论该方法是否成功运行,都会命中 finally 块。
成功消息是一个变量字符串,所以这就是为什么它不在 finally 块中的 "" 中。
我有以下捕获/最终代码:
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show(Success);
}
Run Code Online (Sandbox Code Playgroud)
所以当方法返回错误时,用户仍然会收到操作成功的消息。我该如何解决这个问题?
谢谢。
try-catch ×10
c# ×3
exception ×2
java ×2
php ×2
breakpoints ×1
except ×1
ios ×1
iteration ×1
laravel ×1
loops ×1
powershell ×1
python ×1
sqlexception ×1
swift ×1