Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 2
Jeff,
In a program I have written, the user selects a file to be parsed by a SAX parser. The interface is started in a thread. It then calls methods in other classes in the following order:
functions.parseXML(file) which calls ioHandler.parseXML(file) which calls xmlHandler.readXML(file) which calls xmlFileReader.parse().
The XMLFilHandler class (third in the list) extends Thread and calls xmlFileReader.parse() in its run() method. My problem is when the parser is parsing, the GUI (graphical user interface) pauses then kicks back in once the parsing
is complete. Is this behavior expected with actions that use a lot of processing like parsing?
Toby
Please answer the following questions:
join() method calls wait(0) instead of the sleep(long millis) method. Why?
Last month, I asked you several questions and gave you some exercises to work on. My answers appear in red.
SortIntegerArray1 source code?
The expression, x [j+1], in Line 19 (if (x [j] > x [j+1])) results in an ArrayIndexOutOfBoundsException because a valid ending index was not specified in the inner for loop. As the code currently reads, the inner for loop tests j against x.length-i, when it should test j against x.length-i-1. The correct code appears below:
SortIntegerArray1.java
// SortIntegerArray1.java
class SortIntegerArray1
{
public static void main (String [] args)
{
int numbers [] = { 22, 13, 5, 76, 3 };
sort (numbers);
for (int i = 0; i < numbers.length; i++)
System.out.println (numbers [i]);
}
static void sort (int [] x)
{
for (int i = 0; i < x.length-1; i++)
for (int j = 0; j < x.length-i-1; j++)
if (x [j] > x [j+1])
{
int temp = x [j];
x [j] = x [j+1];
x [j+1] = temp;
}
}
}
static void a () throws FileNotFoundException
{
try
{
throw new FileNotFoundException ();
}
catch (FileNotFoundException e)
{
}
}
Why or why not? Assume the code fragment is from a program that has an import java.io.FileNotFoundException; directive.
The code fragment is legal. Although the method catches the FileNotFoundException, the catch clause can rethrow that object. For that reason, the compiler does not complain that FileNotFoundException is also in the throws clause.
try block appear alone in source code?
No. Either a catch clause or a finally clause must follow a try block.
catch clauses instead of one all-encompassing catch clause?
You should use multiple catch clauses instead of one all-encompassing catch clause to improve code reliability. In an all-encompassing catch clause, you must use if/if-else statements and expressions involving the instanceof operator to determine what kind of exception object was caught so the program can determine which exception-handling code
to execute. You can easily miss something and inadvertently introduce a bug. By using multiple catch clauses, you compartmentalize appropriate exception-handling code in the appropriate catch clauses and lessen the chances of introducing bugs.
Java allows only Throwable and Throwable subclass objects to be thrown.
TranslatedExceptionDemo2 to eliminate the ee.initCause (e); method call, but produce the same output.
To rewrite TranslatedExceptionDemo2 without the call to initCause(), modify the ExternalException constructor to take a Throwable parameter that identifies the exception's cause. Then, pass that parameter's value to the Exception(String message, Throwable cause) constructor. The complete code follows:
TranslatedExceptionDemo2.java
// TranslatedExceptionDemo2.java
class TranslatedExceptionDemo2
{
public static void main (String [] args)
{
try
{
Library.methodA (); // Line 9
}
catch (Library.ExternalException e)
{
System.out.println (e.getMessage ());
// Send stack trace to standard output device, instead of
// default standard error device
e.printStackTrace (System.out);
}
}
}
class Library
{
static void methodA () throws ExternalException
{
try
{
methodB (); // Line 29
}
catch (InternalException e)
{
System.out.println (e.getMessage ());
ExternalException ee = new ExternalException (e); // Line 35
throw ee;
}
}
static class ExternalException extends Exception
{
ExternalException (Throwable cause)
{
super ("External Exception", cause);
}
}
private static void methodB () throws InternalException
{
throw new InternalException (); // Line 50
}
private static class InternalException extends Exception
{
InternalException ()
{
super ("Internal Exception");
}
}
}
See the following source code:
CatchAfterFinally.java
// CatchAfterFinally.java
class CatchAfterFinally
{
public static void main (String [] args)
{
try
{
someMethod1 ();
}
catch (MyException e)
{
System.out.println ("Catching exception: " + e.getMessage ());
}
}
static void someMethod1 () throws MyException
{
try
{
someMethod2 ();
}
finally
{
System.out.println ("Cleaning up");
}
}
static void someMethod2 () throws MyException
{
throw new MyException ("Some exception");
}
}
class MyException extends Exception
{
MyException (String msg)
{
super (msg);
}
}
someMethod2() throws a MyException object. Before the JVM exits the someMethod1() method, in its search for an appropriate catch clause, the JVM executes the finally clause. When run, you see the following output:
Cleaning up Catching exception: Some exception