D
Darryl L. Pierce
It could return a nil value.
Which would imply that there *was* a character there and that it was 0.
So you've now gone from "stupid" to "misleading"...
It could return a nil value.
Thus code like this would even work:
if (line.charAt(0) == nil)
else if (line.charAt(0) == 'a')
else if (line.charAt(0) == 'b')
else if (line.charAt(0) == 'c')
Roedy Green wrote:
It could return a nil value.
The nil could be an object, a value, anything you like,
but the point is, provided the function returned somthing
then you always have something to compared.
Thus code like this would even work:
if (line.charAt(0) == nil)
else if (line.charAt(0) == 'a')
else if (line.charAt(0) == 'b')
else if (line.charAt(0) == 'c')
Jussi said:An example of why Java is a stupid overly complicated
programming language:
import java.io.*;
public class SimpleJavaQuestion
{
public static void main(String[] args)
{
try
{
String line = "";
// surely the first character of the line is not a '#' character
if (line.charAt(0) != '#')
{
System.out.println("Never expect the obvious....");
}
}
catch (Exception e)
{
// stupid me. there is no first character, how obvious!!!!!
System.out.println("When the unexpected is far more interesting.");
}
}
}
[...]Jussi said:An example of why Java is a stupid overly complicated
programming language:
AFAIK, you only catch an exception if you can do something useful to
remedy the situation. In this case, tere should be no catch.
Exceptions aren't caught to catch erors in your code. Remove that
catch.
The NullPointerException, I see no reason to catch. If it occurs, it's
an error on your code.
Raymond said:Actually, catching RuntimeException will prevent the Java app from
dropping completely when the program terminates. While you are
supposed to fix those problems, one or two could potentially slip
through.
Raymond said:[...]Jussi said:An example of why Java is a stupid overly complicated
programming language:
AFAIK, you only catch an exception if you can do something useful to
remedy the situation. In this case, tere should be no catch.
Exceptions aren't caught to catch erors in your code. Remove that
catch.
Even if it is necessairy to catch the exception, it should be as mimimal to
catch only thrown exceptions. (e.g. NullPointerException.)
The NullPointerException, I see no reason to catch. If it occurs, it's
an error on your code.
Actually, catching RuntimeException will prevent the Java app from dropping
completely when the program terminates. While you are supposed to fix
those problems, one or two could potentially slip through.
It's usually best to wrap those catches around optional components or third
party plugins. Other than that (and RTEs that can be generated through
user input), there's not much use for doing so.
Jussi said:I've written several programs this way:
public static void main(String [] args) {
try {
submain(args);
}
catch (Throwable exn) {
write trace to stderr and to a log file (with care)
exit
}
}
This way I can deal with all unexpected exceptions in this one place.
There is nothing to be done other than make sure that I do not lose
that trace.
Babu said:Jussi said:I've written several programs this way:
public static void main(String [] args) {
try {
submain(args);
}
catch (Throwable exn) {
write trace to stderr and to a log file (with care)
exit
}
}
This way I can deal with all unexpected exceptions in this one place.
There is nothing to be done other than make sure that I do not lose
that trace.
And I presume all those applications were single-threaded ? Most
Java apps don't fall in that category - unfortunately.
Jussi said:Babu Kalakrishnan writes:
Jussi said:I've written several programs this way:
public static void main(String [] args) {
try {
submain(args);
}
catch (Throwable exn) {
write trace to stderr and to a log file (with care)
exit
}
}
This way I can deal with all unexpected exceptions in this one place.
There is nothing to be done other than make sure that I do not lose
that trace.
And I presume all those applications were single-threaded ? Most
Java apps don't fall in that category - unfortunately.
Yes, they are all single-threaded. Batch computation, not interactive.
Years ago I tried to set one AWT-using program up this way, and there
was some way to catch exceptions from AWT. I never found out how
official or how portable it was, but sometimes at least it worked.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.