Search Results

Search found 36003 results on 1441 pages for 'try catch'.

Page 3/1441 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • Why doesn't Perl's Try::Tiny's try/catch give me the same results as eval?

    - by sid_com
    Why doesn't the subroutine with try/catch give me the same results as the eval-version does? #!/usr/bin/env perl use warnings; use strict; use 5.012; use Try::Tiny; sub shell_command_1 { my $command = shift; my $timeout_alarm = shift; my @array; eval { local $SIG{ALRM} = sub { die "timeout '$command'\n" }; alarm $timeout_alarm; @array = qx( $command ); alarm 0; }; die $@ if $@ && $@ ne "timeout '$command'\n"; warn $@ if $@ && $@ eq "timeout '$command'\n"; return @array; } shell_command_1( 'sleep 4', 3 ); say "Test_1"; sub shell_command_2 { my $command = shift; my $timeout_alarm = shift; my @array; try { local $SIG{ALRM} = sub { die "timeout '$command'\n" }; alarm $timeout_alarm; @array = qx( $command ); alarm 0; } catch { die $_ if $_ ne "timeout '$command'\n"; warn $_ if $_ eq "timeout '$command'\n"; } return @array; } shell_command_2( 'sleep 4', 3 ); say "Test_2"

    Read the article

  • Java try finally variations

    - by Petr Gladkikh
    This question nags me for a while but I did not found complete answer to it yet (e.g. this one is for C# http://stackoverflow.com/questions/463029/initializing-disposable-resources-outside-or-inside-try-finally). Consider two following Java code fragments: Closeable in = new FileInputStream("data.txt"); try { doSomething(in); } finally { in.close(); } and second variation Closeable in = null; try { in = new FileInputStream("data.txt"); doSomething(in); } finally { if (null != in) in.close(); } The part that worries me is that the thread might be somewhat interrupted between the moment resource is acquired (e.g. file is opened) but resulting value is not assigned to respective local variable. Is there any other scenarios the thread might be interrupted in the point above other than: InterruptedException (e.g. via Thread#interrupt()) or OutOfMemoryError exception is thrown JVM exits (e.g. via kill, System.exit()) Hardware fail (or bug in JVM for complete list :) I have read that second approach is somewhat more "idiomatic" but IMO in the scenario above there's no difference and in all other scenarios they are equal. So the question: What are the differences between the two? Which should I prefer if I do concerned about freeing resources (especially in heavily multi-threading applications)? Why? I would appreciate if anyone points me to parts of Java/JVM specs that support the answers.

    Read the article

  • Try::Tiny-Question

    - by sid_com
    Why doesn't the subroutine with try/catch give me the same results as the eval-version does. #!/usr/bin/env perl use warnings; use strict; use 5.012; use Try::Tiny; sub shell_command_1 { my $command = shift; my $timeout_alarm = shift; my @array; eval { local $SIG{ALRM} = sub { die "timeout '$command'\n" }; alarm $timeout_alarm; @array = qx( $command ); alarm 0; }; die $@ if $@ && $@ ne "timeout '$command'\n"; warn $@ if $@ && $@ eq "timeout '$command'\n"; return @array; } shell_command_1( 'sleep 4', 3 ); say "Test_1"; sub shell_command_2 { my $command = shift; my $timeout_alarm = shift; my @array; try { local $SIG{ALRM} = sub { die "timeout '$command'\n" }; alarm $timeout_alarm; @array = qx( $command ); alarm 0; } catch { die $_ if $_ ne "timeout '$command'\n"; warn $_ if $_ eq "timeout '$command'\n"; } return @array; } shell_command_2( 'sleep 4', 3 ); say "Test_2"

    Read the article

  • Is your TRY worth catching?

    - by Maria Zakourdaev
      A very useful error handling TRY/CATCH construct is widely used to catch all execution errors  that do not close the database connection. The biggest downside is that in the case of multiple errors the TRY/CATCH mechanism will only catch the last error. An example of this can be seen during a standard restore operation. In this example I attempt to perform a restore from a file that no longer exists. Two errors are being fired: 3201 and 3013: Assuming that we are using the TRY and CATCH construct, the ERROR_MESSAGE() function will catch the last message only: To workaround this problem you can prepare a temporary table that will receive the statement output. Execute the statement inside the xp_cmdshell stored procedure, connect back to the SQL Server using the command line utility sqlcmd and redirect it's output into the previously created temp table.  After receiving the output, you will need to parse it to understand whether the statement has finished successfully or failed. It’s quite easy to accomplish as long as you know which statement was executed. In the case of generic executions you can query the output table and search for words like“Msg%Level%State%” that are usually a part of the error message.Furthermore, you don’t need TRY/CATCH in the above workaround, since the xp_cmdshell procedure always finishes successfully and you can decide whether to fire the RAISERROR statement or not. Yours, Maria

    Read the article

  • I can't boot into Ubuntu "Try (hd0,0): NTFS5: No ang0" Error Message

    - by Joe
    I recently installed Ubuntu 12.04 alongside windows 7. It was working fine but now when I try to boot with ubuntu after the operating system choice screen I get this. Boot Error Message Try (hd0,0): NFTS5: No ang0 Try (hd0,1): NTFS5: No ang0 Try (hd0,2): NTFS5: No ang0 Try (hd0,3): Extended: Try (hd0,4): NTFS5: No ang0 Try (hd0,5): Extended: Try (hd0,5): EXT2: And when I press ctrl+alt+del it restarts the computer and if I chose to boot with ubuntu same thing happens again. But windows works fine.. How do I resolve this problem? Thanks.

    Read the article

  • Why can't I write just a try with no catch or finally?

    - by Camilo Martin
    Sometimes I do this and I've seen others doing it too: VB: Try DontWannaCatchIt() Catch End Try C#: try { DontWannaCatchIt(); } catch {} I know I should catch every important exception and do something about it, but sometimes it's not important to - or am I doing something wrong? Is this usage of the try block incorrect, and the requirement of at least one catch or finally block an indication of it?

    Read the article

  • Validating data to nest if or not within try and catch

    - by Skippy
    I am validating data, in this case I want one of three ints. I am asking this question, as it is the fundamental principle I'm interested in. This is a basic example, but I am developing best practices now, so when things become more complicated later, I am better equipped to manage them. Is it preferable to have the try and catch followed by the condition: public static int getProcType() { try { procType = getIntInput("Enter procedure type -\n" + " 1 for Exploratory,\n" + " 2 for Reconstructive, \n" + "3 for Follow up: \n"); } catch (NumberFormatException ex) { System.out.println("Error! Enter a valid option!"); getProcType(); } if (procType == 1 || procType == 2 || procType == 3) { hrlyRate = hrlyRate(procType); procedure = procedure(procType); } else { System.out.println("Error! Enter a valid option!"); getProcType(); } return procType; } Or is it better to put the if within the try and catch? public static int getProcType() { try { procType = getIntInput("Enter procedure type -\n" + " 1 for Exploratory,\n" + " 2 for Reconstructive, \n" + "3 for Follow up: \n"); if (procType == 1 || procType == 2 || procType == 3) { hrlyRate = hrlyRate(procType); procedure = procedure(procType); } else { System.out.println("Error! Enter a valid option!"); getProcType(); } } catch (NumberFormatException ex) { System.out.println("Error! Enter a valid option!"); getProcType(); } return procType; } I am thinking the if within the try, may be quicker, but also may be clumsy. Which would be better, as my programming becomes more advanced?

    Read the article

  • PowerShell Try Catch Finally

    - by PointsToShare
    PowerShell Try Catch Finally I am a relative novice to PowerShell and tried (pun intended) to use the “Try Catch Finally” in my scripts. Alas the structure that we love and use in C# (or even – shudder of shudders - in VB) does not always work in PowerShell. It turns out that it works only when the error is a terminating error (whatever that means). Well, you can turn all your errors to the terminating kind by simply setting - $ErrorActionPreference = "Stop", And later resetting it back to “Continue”, which is its normal setting. Now, the lazy approach is to start all your scripts with: $ErrorActionPreference = "Stop" And ending all of them with: $ErrorActionPreference = "Continue" But this opens you to trouble because should your script have an error that you neglected to catch (it even happens to me!), your session will now have all its errors as “terminating”. Obviously this is not a good thing, so instead let’s put these two setups in the beginning of each Try block and in the Finally block as seen below: That’s All Folks!!

    Read the article

  • Zend - unable to catch exception [closed]

    - by coder3
    This still throw an uncaught exception.. Any insight why this isn't working? protected function login() { $cart = $this->getHelper('GetCurrentCart'); $returnValue = false; if ($this->view->form->isValid($this->_getAllParams())) { $values = $this->view->form->getValues(); try { $this->goreg = $this->goregFactory->create($this->config->goreg->service_url); if ($this->goreg->login($values['username'], $values['password']) && $this->goregSession->isLoggedIn()) { $returnValue = true; } else { echo 'success 1'; } } catch (Exception $e) { echo 'error 1'; } catch (Zend_Exception $e) { echo 'error 2'; } catch (Zend_Http_Client_Exception $e) { echo 'error 3'; } } return $returnValue; }

    Read the article

  • SQL Server Begin Try

    - by Derek Dieter
    The try catch methodology of programming is a great innovation for SQL 2005+. The first question you should ask yourself before using Try/Catch should be “why?”. Why am I going to use Try/Catch? Personally, I have found a few uses, however I must say I do fall into the category of not [...]

    Read the article

  • Should try...catch go inside or outside a loop?

    - by mmyers
    I have a loop that looks something like this: for(int i = 0; i < max; i++) { String myString = ...; float myNum = Float.parseFloat(myString); myFloats[i] = myNum; } This is the main content of a method whose sole purpose is to return the array of floats. I want this method to return null if there is an error, so I put the loop inside a try...catch block, like this: try { for(int i = 0; i < max; i++) { String myString = ...; float myNum = Float.parseFloat(myString); myFloats[i] = myNum; } } catch (NumberFormatException ex) { return null; } But then I also thought of putting the try...catch block inside the loop, like this: for(int i = 0; i < max; i++) { String myString = ...; try { float myNum = Float.parseFloat(myString); } catch (NumberFormatException ex) { return null; } myFloats[i] = myNum; } So my question is: is there any reason, performance or otherwise, to prefer one over the other? EDIT: The consensus seems to be that it is cleaner to put the loop inside the try/catch, possibly inside its own method. However, there is still debate on which is faster. Can someone test this and come back with a unified answer? (EDIT: did it myself, but voted up Jeffrey and Ray's answers)

    Read the article

  • Check If You Are Eligible To Try Amazon Fire TV Free for 30 days

    - by Gopinath
    Re/Code first broke the story of Amazon offering its customers to try Amazon Fire TV free for 30 days. The Amazon Fire TV costs $99 but with this offer you get a chance to try it for 30 days without paying any money. After 30 days if you are interested you can keep it with you by paying $99 otherwise you can return it to Amazon. All the costs associated with shipping to your home as well as returning back to Amazon are covered by the offer, which means it’s a real FREE offer to try! This offer is not available for everyone, but selected customers are receiving emails from Amazon. If you are interested to try free Amazon TV either you may wait to receive an offer email or click on this link to check if you are eligible. I verified it today and it says that I’m not eligible for this offer. Try your luck and all the best.

    Read the article

  • Arguments for or against using Try/Catch as logical operators

    - by James P. Wright
    I just discovered some lovely code in our companies app that uses Try-Catch blocks as logical operators. Meaning, "do some code, if that throws this error, do this code, but if that throws this error do this 3rd thing instead". It uses "Finally" as the "else" statement it appears. I know that this is wrong inherently, but before I go picking a fight I was hoping for some well thought out arguments. And hey, if you have arguments FOR the use of Try-Catch in this manner, please do tell. EDIT For any who are wondering, the language is C# and the code in question is about 30+ lines and is looking for specific exceptions, it is not handling ALL exceptions.

    Read the article

  • Why is this SocketException not caught by a generic catch routine?

    - by Tarnschaf
    Our company provides a network component (DLL) for a GUI application. It uses a Timer that checks for disconnections. If it wants to reconnect, it calls: internal void timClock_TimerCallback(object state) { lock (someLock) { // ... try { DoConnect(); } catch (Exception e) { // Log e.Message omitted // Raise event with e as parameter ErrorEvent(this, new ErrorEventArgs(e)); DoDisconnect(); } // ... } } So the problem is, inside of the DoConnect() routine a SocketException is thrown (and not caught). I would assume, that the catch (Exception e) should catch ALL exceptions but somehow the SocketException was not caught and shows up to the GUI application. protected void DoConnect() { // client = new TcpClient(); client.NoDelay = true; // In the following call the SocketException is thrown client.Connect(endPoint.Address.ToString(), endPoint.Port); // ... (login stuff) } The doc confirmed that SocketException extends Exception. The stacktrace that showed up is: TcpClient.Connect() -> DoConnect() -> timClock_TimerCallback So the exception is not thrown outside the try/catch block. Any ideas why it doesn't work?

    Read the article

  • Unhandled exception, even after adding try-catch block ? C++

    - by user2525503
    try { bool numericname=false; std::cout <<"\n\nEnter the Name of Customer: "; std::getline(cin,Name); std::cout<<"\nEnter the Number of Customer: "; std::cin>>Number; std::string::iterator i=Name.begin(); while(i!=Name.end()) { if(isdigit(*i)) { numericname=true; } i++; } if(numericname) { throw "Name cannot be numeric."; } } catch(string message) { cout<<"\nError Found: "<< message <<"\n\n"; } Why am I getting unhandled exception error ? Even after I have added the catch block to catch thrown string messages?

    Read the article

  • How to configure catch-all in Exchange2010 hub-transport environment?

    - by Itay Levin
    Im getting the delivery failed from the post master reply. i don't want it. because then poeple can find out all my real users on the exchange. also, i have a lot of users (10K) in my application - and i don't want to create a mailbox for each user. is it possible to get this done in ex2010 sp1. hub-transport configuration? or i must use edge-transport as indicated in http://technet.microsoft.com/en-us/library/bb691132(EXCHG.80).aspx

    Read the article

  • Quadcopters Play Catch [Video]

    - by Jason Fitzpatrick
    Working like a group of hive-minded bees, these quadcopters come off as almost playful with their ball throwing antics. Courtesy of the folks at the Swiss Federal Institute of Technology in Zurich’s Institute for Dynamic Systems and Control, we’re treated to a video of three quadcopters playing catch in the research facility’s Flying Machine Area. They explain the processes demonstrated in the video: This video shows three quadrocopters cooperatively tossing and catching a ball with the aid of an elastic net. To toss the ball, the quadrocopters accelerate rapidly outward to stretch the net tight between them and launch the ball up. Notice in the video that the quadrocopters are then pulled forcefully inward by the tension in the elastic net, and must rapidly stabilize in order to avoid a collision. Once recovered, the quadrotors cooperatively position the net below the ball in order to catch it. Because they are coupled to each other by the net, the quadrocopters experience complex forces that push the vehicles to the limits of their dynamic capabilities. To exploit the full potential of the vehicles under these circumstances requires several novel algorithms, including: HTG Explains: How Antivirus Software Works HTG Explains: Why Deleted Files Can Be Recovered and How You Can Prevent It HTG Explains: What Are the Sys Rq, Scroll Lock, and Pause/Break Keys on My Keyboard?

    Read the article

  • why does Visual Studio not enforce try-catch-block implementation?

    - by Pedro
    Coming from Eclipse/Java, I noticed that in VisualStudio/C# it is not mandatory to care about Exceptions. While Eclipse forces the user to implement a try-catch-block or to add a throws declaration, this is not the case in Visual Studio. What is the reason Visual Studio doesn't inform about unhandled exceptions? Can I configure Visual Studio to force me to implement try-catch-blocks, or at least add a compiler-warning?

    Read the article

  • Finding the try for an except or finally [migrated]

    - by ?s?
    I'm dealing with some code that has fantastically long methods (10k lines!) and some odd use of try-finally and try-except blocks. Some of the latter are long by themselves, and don't always have the try at the start of the method. Obviously I'm trying to refactor the code, but in the meantime just being able to fix a couple of common pathologies would be much easier if I could jump to the start of a block and see what is happening there. When it's 20+ pages away finding it even with the CNPack rainbows is just tedious. I'm using D2010 and have GExperts (with DelForExp), CNPack and DDevExtensions installed, but I can't find anything that lets me jump from the try to the finally or back. Am I missing something? Is there another add-in that I can use that will get me this?

    Read the article

  • Can you catch exceeded allocated memory error before it kills the script?

    - by kristovaher
    The thing is that I want to catch memory problems before they happen. I have a system that gets rows from database and casts the returned associative array to a variable, but I never know what the size of the database result is is or how much memory it will take once the database request is made. This means that my software can fail simply because memory is exceeded. But I want to avoid that somehow. One of the ways is to obviously make database requests that are smaller, but what if this is not possible or what if I do not know the size of data that is returned from database? Is it possible to 'catch' situations where memory use is exceeded in PHP? Something like this: $requestOk=memory_test(function(){ return doSomething(); }); if($requestOk){ // Memory seems fine // $requestOk now has the value from memory_test() function } else { // Function would have exceeded memory } I just find it problematic that my script can just die at any moment because of memory issues. From what I know, try-catch cannot be used here because it is a fatal error. Any help would be appreciated!

    Read the article

  • [Blog] N'utilisez jamais try {} catch {}

    C'est en somme le conseil donn? par Karl Seguin sur son blog. Pour lui, l'utilisation du try/catch pour g?rer une exception est une mauvaise pratique : "If an exception happens, you need to know about it. If a truly unexpected exception happens, you're better off (most of the time) crashing than letting the application continue (...) The best way to achieve both is let the exception go unhandled and log the exception in a global exception handler". Pour paraphraser une ?mission c?l?bre, ?a se discute ...

    Read the article

  • microsoft windows driver kit pure C try catch syntax ?

    - by clyfe
    In the Windows Driver Kit (WDK) there are some driver code samples written in pure C, but sprinkled with some try-catch-finally constructs. Does someone know their semantics ? Thank you microsoft for your great tools and standards compliance. Code extract from some_file.c: try { ... if (!NT_SUCCESS( status )) { leave; // ??? } ... } finally { ... } try { ... } except( EXCEPTION_EXECUTE_HANDLER ) { ... }

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >