try-catch

How should I replicate the functionality of C#'s 'using' keyword in Java?

I'm converting some C# code to Java and it contains the using keyword. How should I replicate this functionality in Java? I was going to use a try, catch, finally block but I thought I'd check with you guys first. ...

Should a function be used in try block that has to be executed in finally block.

try { OpenConnection(); RowsAffected = cmd.ExecuteNonQuery(); CloseConnection(true); //should I use this function call here //as well, when I am using it in finally //block. For closing database connection. } catch (SqlException ex) { throw ex; } finally { CloseConnection(true); } Or Should I write it this way try { Ope...

Throws or try-catch

What is the general rule of thumb when deciding whether to add a throws clause to a method or using a try-catch? From what I've read myself, the throws should be used when the caller has broken their end of the contract (passed object) and the try-catch should be used when an exception takes place during an operation that is being carri...

Does the C# "finally" block ALWAYS execute?

Possible Duplicate: Will code in a Finally statement fire if I return a value in a Try block? Consider the following code C# code. Does the "finally" block execute? public void DoesThisExecute() { string ext = "xlsx"; string message = string.Empty; try { switch (ext) { case "xls": message = "Great choi...

Javascript Try-Catch Performance Vs. Error Checking Code

Hello everybody! Would it be faster to just put code inside a try-catch block instead of performing various error checks? For example.. function getProjectTask(projectTaskId) { if (YAHOO.lang.isUndefined(projectTaskId) || YAHOO.lang.isNull(projectTaskId) && !YAHOO.lang.isNumber(projectTaskId)) { return null; } var...

Call-stack for exceptions in C++

Today, in my C++ multi-platform code, I have a try-catch around every function. In every catch block I add the current function's name to the exception and throw it again, so that in the upmost catch block (where I finally print the exception's details) I have the complete call stack, which helps me to trace the exception's cause. Is it...

A better way to validate URL in C# than try-catch?

I'm building an application to retreive an image from internet. Even though it works fine, it is slow (on wrong given URL) when using try-catch statements in the application. (1) Is this the best way to verify URL and handle wrong input - or should I use Regex (or some other method) instead? (2) Why does the application try to find im...

Checking if memory is allocated before calling FreeMem (Delphi 32)

I have been running into errors where objects are somehow freed but we end up calling FreeMem on them. Of course this causes an error since the memory has already been freed and throws an error. I know that a try-catch block would probably fix it but that's a lot of try-catch blocks. With the regular object.free the way to avoid this ...

Problem catching exceptions

Hey hey! I'm making an iphone based app and I have issues catching exceptions. So far, I've never had problem with try catches but here... well :D Here is the code that doesn't catch any exception : - (void)updateView:(NSTimer*)t { NSMutableDictionary *requestResult = [[[NSMutableDictionary alloc] init] autorelease]; @try {...

tcl: capture output from "exec diff" which returned non-zero

Hi, I know it is common to use catch when executing commands that may return non-zero... but how can I get the output in that case? To be specific, I wish to do something like "catch {exec diff fileA fileB} ret". The files are different and ret value is 1. What I actaully need is the output of diff, the detailed differences. But I bel...

Rethrowing an Exception causing containing Catch to reexecute

Using PHP 5.3.2 I'm handling an Exception in a catch block. After logging the error, I want to pass it on for the Framework to handle. When I try to rethrow the exception, throw $e, the current catch block is reexecuted causing a duplicate entry in my log and then the exception is passed on up. ...

Is there a "Try" convention?

I sometimes see methods in the .net framework prefixed with "Try" e.g. int.TryParse(..). I assume this means that the method is the same as a int.parse, but wrapped in a try catch? Does this mean that if I write methods which have a try catch around them (e.g logging, which I never want to raise an exception), they should be prefixed w...

Determine if executing in finally block due to exception being thrown

Is it possible to determine if code is currently executing in the context of a finally handler as a result of an exception being thrown? I'm rather fond of using the IDisposable pattern to implement entry/exit scoping functionality, but one concern with this pattern is that you might not necessarily want the end-of-scope behavior to occ...

Using "Throw" in a catchblock (and nothing else!)

Possible Duplicate: difference between throw and throw new Exception() I'm a programmer working on adding new functionality to legacy code. While debugging, I parsed over this Catch block, which got an angry "object not set to reference of object" notice from Visual Studio: catch(Exception ex) { Sp...

How should I initialize variables that will be used in a try/catch/finaly block?

If I am using a try/catch/finally block where and how should I initialize variables? For example say I'm trying to use a FileStream . I want to catch any exceptions thrown while creating or using the stream. Then regardless of whether there were any problems or not I want to ensure any stream created is closed. So I'd do something like ...

Try Catch with PHP warnings

Is it possible to do some sort of try catch that will catch warnings? e.g. if (!$dom->loadHTMLFile($url)) { //if cant load file handle error my way } For the $url I am using I am getting Warning (2): DOMDocument::loadHTMLFile(MYURL) [domdocument.loadhtmlfile]: failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden ...

Difference between GOTO and THROW?

Many people accused me recently for just mentioning a single word - "goto". It makes me wonder, why it is considered such a nasty word. I am aware of several previous discussions on the topic, but it doesn't convince me - some of the answers just says "it's bad" not even trying to explain and some bring reasons, irrelevant for scripting ...

java compiler says this exception is never thrown in body of corresponding try statement - but it _is_ thrown

I have the following code: try { //jaw-ws service port operation port.login(); } catch (Exception e) { logger.error("Caught Exception in login(): " + e.getMessage()); } When the above is run with an incorrect hostname, I get: Caught Exception in login(): HTTP transport error: java.net.UnknownHostException: abc That is c...

php try catch wsod

I have an issue. When I run: try { $as ->setForename($_POST['fname']) ->setSurname($_POST['sname']) ->setEmail($_POST['email']) ->setUser($_POST['user']) ->setPass($_POST['pw']) ->setPhone($_POST['tel']) ->setMobile($_POST['mob']) ->setJob($_POST['job']) ->setAuth($_POST['auth']) ->addProcess(); } catch (Exception $...

Can you catch in a using block?

Can exceptions be caught inside a using block, and if so what is the syntax? So, something like the following: using (var creatingThing = new MyCreatingThing()) { creatingThing.CreateSomething(); catch() { creatingThing.Rollback(); } } Can this be done? Or do I need to write this code manually (ie without a ...