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.
...
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...
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...
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...
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...
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...
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...
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 ...
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 {...
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...
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.
...
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...
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...
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...
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 ...
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
...
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 ...
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...
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 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 ...