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 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"));
...
Hi
What does it mean in .Net: try-catch block without any Exception as parameter for catch?
...
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...
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...
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.
...
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...
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'));
} ...
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.
...
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 ...
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...
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...
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...
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...
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, 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...
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...
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...
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...
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...