try-catch

advantages and disadvantages of using try/catch [closed]

懵懂的女人 提交于 2019-12-12 04:59:33
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I want to ask what is the advantage and disadvantages of using try/catch ? and when I must use it and when I must not use it ? 回答1:

How to catch plink failure when host not known

▼魔方 西西 提交于 2019-12-12 04:56:24
问题 I have a problem with one of my scripts. Basically it runs very smoothly but it needs some user input from an excel file. Users can cause Errors and thats my problem. My Script connects via ssh with plink (lil PuTTY) to certain devices and sends them commands. Now to the real problem: if someone just misspells the FQDN, the script will go on but I need to be reported via Email that one process has failed. If someone entirely screws up the Excel-Sheet the whole Script will fail - and I need to

Try/catch not working in Powershell console, but works in ISE

谁都会走 提交于 2019-12-12 04:32:07
问题 I have a Powershell script with try / catch parts to write the errors into a logfile. This works perfectly fine in Powershell ISE, but not in the Powershell console. Here's the script: $serverfile = "C:\Serverlist.txt" $Logfile = "C:\Deploy_Log.txt" $startdate= "01/05/2016" $starttime = 13 Set-StrictMode -Version latest $ErrorActionPreference = "Stop" $WarningPreference = "Stop" function LogWrite { param([string]$logstring) Add-Content $Logfile -Value $logstring } function DeployTask { param(

Break doesn't work in try with resources, but work in try without resources

别来无恙 提交于 2019-12-12 04:12:42
问题 Break doesn't work in try with resources, but work in try without resources! This is a simple example for this situation. I caught this "bug" in work project. When I use try without resources try { Resources resources = getResources() // some code here, look below } I have only one iteration of my cycle, and it's right because I have the condition "if true then break", but when I changed try without recourse on try WITH resources. try (Resources resources = getResources()) { // some code here

Swift 2: Throw from closure

╄→гoц情女王★ 提交于 2019-12-12 03:38:41
问题 I'm upgrading my code to Swift 2 using error handling with try-catch. I've stuck with closure (NSURLSession), I can't throw inside it. Generally I'm using such code: let request = NSURLRequest() let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { throw(ErrorType) // here some errortype enum } } But I'm receiving the error: "Cannot invoke dataTaskWithRequest with an argument list of type …". How can I throw from closure? 回答1: You

Adding numbers in a string

跟風遠走 提交于 2019-12-12 03:29:54
问题 I have a string as an input for the code I'm writing and let an example of the string be: "12 inches makes 1 foot" My goal is to have my code run through this string and just pull out the integers and add them. So the output for the string above would be 13. I am using try and except in here as well since another sample input string could be something like "pi is 3.14". msg= "12 inches makes 1 foot" thesum = 0 s= msg.split() for a in s: try: if a == int(a): a= int(a) thesum += a print (thesum

Powershell catches wrong exception? What am I doing wrong?

帅比萌擦擦* 提交于 2019-12-12 03:17:33
问题 Last week I ran into a strange issue. I modified code that should test if a file is locked and which I once found on the Internet (essentially similar to https://superuser.com/questions/876288/how-do-i-detect-and-skip-locked-files-in-a-powershell-script) to 1) deal with timeout and 2) to handle non existing files (and directories) correctly. So here is my code: function Test-IsFileLocked { [CmdletBinding()] Param ( [Parameter(Mandatory = $True, Position = 0)] [ValidateNotNullOrEmpty()]

VB.net Try Catch. Keep the Loop after the Catch

徘徊边缘 提交于 2019-12-12 02:56:28
问题 I'm using Try Catch and I want to add into Form3.ListBoxes the items that are into Form2.ListBoxes in other type. But it stop adding after the Catch the exception. So I want to keep the Loop after the exception be caught! My program get products and show the same products but in another type (Like: I have a T-shirt with a brand, but I want the "same" T-shirt in another brand). ListBox5 are the quantity that I add in Form1. I load Images to be clearly. Form2 Listboxes are in order (ListBox1

Django: User-defined exception not being caught

拥有回忆 提交于 2019-12-12 02:53:47
问题 I have a user-defined exception in my Django app: class TemplateMatchError(Exception): ... I have a segment of code that catches this exception in regular try ... except : try: if template.match(text): return attrs except TemplateMatchError as e: continue I noticed that in production, when DEBUG=True , this error is not caught and if raised, my browser displays the yellow Django stack trace page. When DEBUG=False , the exception is caught. I was surprised by this behavior since it means the

Why wont my program execute the second catch block, when the error exists?

混江龙づ霸主 提交于 2019-12-12 02:49:34
问题 I am new to try/catch exception handling, and am wondering why my second catch block will not execute. The sec variable should not be between 0-59, so I'd like it to say "invalid second entry", but it doesn't. Thank you! #include <stdexcept> #include <iostream> #include <string> using namespace std; class BadHourError : public runtime_error { public: BadHourError() : runtime_error("") {} }; class BadSecondsError : public runtime_error { public: BadSecondsError() : runtime_error("") {} };