try-catch

try-catch problem

Hey guys, I am a java newbie, my question is about try-catch blocks on a simple division by zero example. You see the first line of try? If I cast any of those two variables to the double the program does not recognize the catch block. In my opinion, whether I cast or not only the catch block must be executed. What is wrong on this code...

Why Java return statment inside the catch block not working?

Why does the following code always return true even when an exception is thrown? public boolean write (ArrayList<String> inputText, String locationToSave) { try { File fileDir = new File(locationToSave); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileDir), "utf8")); ...

What does it mean in .Net: try-catch block without any Exception as parameter for catch?

Hi What does it mean in .Net: try-catch block without any Exception as parameter for catch? ...

While loop with try catch fails at bad cin input

I can't seem to figure out why this falls into a loop after getting non-int input. I've tried cin.flush(), which doesn't seem to exist, cin.clear(), which seems like it should work, even cin.sync() after reading someone else post about it working, but didn't seem to make much sense. Also tried cin.bad(). Thank you very much for any help...

Can I have IO exception when I create a server socket in Java?

I have the following code: Socket clientSocket = null; try { clientSocket = serverSocket.accept(); } catch (IOException e) { System.err.println("Accept failed."); System.exit(1); } The code is taken from a java.sun.com. I have several questions concerning the above given short part of the code. Why do we want to catch an I...

Php - check if an include or block of code has an error...

How would I go about checking if and include or a require has an error in it. For example, and include would try to be included, if that page has an error the page isn't included and a message is throw? Cheers. ...

How do I get .try() working in a Rails module?

With Rails 2.3.5 I've written a module in RAILS_ROOT/lib/foo.rb which I'm including in some models via "include Foo" and all is well except where I try to use some_object.try(:some_method) in the module code - it throws a NoMethodError rather than returning nil like it would from a Rails model/controller/etc. Do I need to require a Rail...

PHP Try-Catch Failing to Catch (Not a 'Warning')

I have some PHP code that should cause and catch two exceptions: try{ @$this->connector->connect(); // Suppress the default warning (doesn't effect 'throw') } catch(BadArgumentException $e) {} // Works, no error, following code executes. try{ @$this->connector->connect(array('user' => 'Doesn\'t exist', 'pass' => 'invalid')); } ...

How to catch segmentation fault in Linux?

I need to catch segmentation fault in third party library cleanup operations. This happens sometimes just before my program exits, and I cannot fix the real reason of this. In Windows programming I could do this with __try - __catch. Is there cross-platform or platform-specific way to do the same? I need this in Linux, gcc. ...

Catching "Run-Time Check Failure #0 - The value of ESP was not properly "

So, I am calling a function from an unmanaged .dll file from my C# code. Depending on the arguments passed to that function, it can cause "Run-Time Check Failure #0 - The value of ESP was not properly " error.This is completely normal behavior for the function ( yes , I know this sounds VERY strange, but bear with me ). However, if this ...

C#: Equivalent of the python try/catch/else block

In Python, there is this useful exception handling code: try: # Code that could raise an exception except Exception: # Exception handling else: # Code to execute if the try block DID NOT fail I think it's useful to be able to separate the code that could raise and exception from your normal code. In Python, this was possib...

Testing In A Try Catch With Moq Compared To Rhino Mocks

I've just been working on some tests using Moq but ran into trouble trying to test a method I wanted to call twice through a try catch block. The principle is that the first call throws an exception, then in the catch I correct the problem and call the method again. I managed to do it with Rhino Mocks as below but being new to both fra...

Stop ArrayOutOfBoundsException from halting program execution in Java

Im am currently developing an automated "test" class (running several individual tests on other classes in the same package). The aim of the test file is to show whether each test either passed or failed. Some of the files being tested are not written correctly creating an ArrayOutOfBoundsException when running the test, that produces my...

Try-Catch-Throw in the same Java class

Is it possible to catch a method in the current class the try-catch block is running on? for example: public static void arrayOutOfBoundsException(){ System.out.println("Array out of bounds"); } ..... public static void doingSomething(){ try { if(something[i] >= something_else); } catch (arrayOut...

Java Try and Catch IOException Problem

Hi all, I am trying to use a bit of code I found at the bottom of this page. Here is the code in a class that I created for it: import java.io.LineNumberReader; import java.io.FileReader; import java.io.IOException; public class LineCounter { public static int countLines(String filename) throws IOException { LineNumberReader re...

Is it better to use try/catch instead of multiple IF statements?

Is it better, less expensive or more readable to use a try/catch block in Java instead of using multiple If statements to check user input for example? Example when parsing a Date string, won't it be better to parse directly using a try/catch block instead of writing multiple statements looking for illegal characters. In another examp...

pros and cons of TryCatch versus TryParse

What are the pros and cons of using either of the following approaches to pulling out a double from an object? Beyond just personal preferences, issues I'm looking for feedback on include ease of debugging, performance, maintainability etc. public static double GetDouble(object input, double defaultVal) { try { return Co...

javascript - catch SyntaxError and run alternate function

Hello there, I'm trying to build something on javascript that I can have an input that can be everything like string, xml, javascript and (non-javascript string without quotes) as follows: //strings eval("'hello I am a string'"); /* note the following proper quote marks */ //xml eval(<p>Hello I am a XML doc</p>); //javascri...

How can I print the argument value that caused Exception in Java?

I am writing a parser for csv-files, and sometimes I get NumberFormatException. Is there an easy way to print the argument value that caused the exception? For the moment do I have many try-catch blocks that look like this: String ean; String price; try { builder.ean(Long.parseLong(ean)); } catch (NumberFormatException e) { Sy...

How convince other developers not to ignore Exceptions?

Recently I encountered a bug in an application I took over from another developer. I debugged for the reason and over an hour later I realized, that the problem wasn't the code producing the exception, but some code executed before this returning wrong data. If I dived into this, I encountered the following: try { ... } catch (XYExcep...