Re: Introspect whether setCharacterEncoding is present..

Nic Ferrier <[email protected]> 13 Oct 2004 00:33:41 +0100
Newsgroups gmane.comp.java.advanced-servlets
Message-ID <[email protected]>
<[email protected]> writes:

> First of all, nice to see this list active again, with something
> other than Job offerings (otherwise referred to as Spam). Even if it
> is about something as offtopic as the proper use of
> Exceptions. (More like Basic-Coding-Practices, no?)
> 
> Also, nice to see Nic back to form again. For a while there, I'd
> been wondering if he'd actually learnt the art of tact...

Next week I'm going to be taking up my post as Donald Rumsfeld's plain
speaking coach.



> > > Use booleans and ifs. Unless it is in a non-critical path, then do
> > > whatever you like. However, in a non-critical path, one may write how bad
> > > code one desires, as it'll execute very seldom.
> 
> > Rubbish. You can't use booleans and ifs to do non-local exits.
> 
> No one said anything about non-local exits. 

Err... I did. Several times. They are a major reason for using
Exceptions other than error handling. They give the lie to the
statement that you must not use Exceptions for control flow.


> The point is that using Exceptions for Critical Paths is not such a
> good idea.

As I said. This is nonsense. If your code happens to be a large FSM
then you are going to need non-local exits in the "Critical Paths" in
your code. This is the only way to make such code readable, the only
other way to write it is as one large method. And that isn't a good
idea (also you might need the method call stack anyway).

What is a critical path anyway? Which bits of my program can I leave
out because they're not critical? Grrrr...


> They make your code harder to read. 

No they don't. They CAN make your code harder to read. But if you
didn't use them in some situations then your code would be much harder
to read than the other possibilities.

And do you really think that Charles' example was hard?

  try
  {
    DoSomething();
  }
  catch (Exception e)
  {
    DoSomethingElseDependentOnFailureOfSomething();
  }

I don't. I've managed lots of code like this written by other
people. I also write and manage it myself.


All I'm saying is that there is an application for Exceptions other
than errors. You CAN use Exceptions for non-local exits and they are
not expensive. Of course, you have to take care. But you should take
care about the way you right programs anyway.


> I do think that Exceptions and Non-local Exits are synonymous. But
> that still doesn't make them a _preferred_ way of doing things. You
> should only use a non-local exit, when the situation warrants it.

This is the bit I have trouble understanding. Perhaps people are not
understanding *me*.

What I am asserting is that the statement "Exceptions must not be used
for control flow" is incorrect. There are situations:

- where you have to use Exceptions for control flow 
- where you should use Exceptions for control flow

These are:

- multi method FSMs

- predicating behaviour on assertion by Exception


Clearly if you can't do something any other way it makes the only
alternative the preferred way. But perhaps you mean it's not
preferential but we have to do it.


> Yes, but was the example that triggered this entire debate one of
> those? Admitted, it's the only obvious way to check if the class
> exists in the current JVM, but Class.forName() is really intended to
> get an instance of a class if you expect it to be there, and not as
> a mechanism to check if the class exists.

The only obvious way? Or the only way?


> The fact that the Java standard API doesn't give you a better way to
> check for the existence of a class without throwing an exception (it
> doesn't, right? Just checking.) does NOT make the suggested way, The
> _Right_ Way. It just makes it a convenient hack to get around the
> limitations of the existing API - also known as, The Only Way.

What? What planet are you living on? There is no other way. It's not a
cruddy way to do it: it's THE way to do it. What are we to do when
writing programs? Not do something because it's inelegant?

Inelegant and inadvisable are different things.


> What is a "non-local exit"? What is an "If" check? And what is the
> requirement here? All we're looking for, is the existence of a
> class.

Non-local exits got included in this thread because the suggestion was
made that Exceptions should not be used for control flow. Non-local
exits were an obvious and legitimate use for Exceptions that made the
suggestion a lie.

 

> Right. You really _really_ don't like support engineers, do you?
> (No, I'm not a support engineer either, but I like to think of
> myself as the sensitive type)

Actually I do a bit of that from time to time. I'm a hacker. People
hire me to sort stuff out. I have to read people's code and manage it
all the time. And I find that reading the code rather than the log
statements is the most sensible tactic.

There really is nothing wrong with goto, Sun call it continue or break
and they use it quite often in JDK code I'm told.


> And as far as the performance aspect goes, I don't disagree with Nic
> there. Most times, the effect of throwing Exceptions, on
> performance, is negligible.

Good man.


The only other way I can see of doing Charles' code is this:

  boolean exists = false;
  try
  {
    Class.forName("com.bull.LoadOf");
    exists = true;
  }
  catch (...Exception e)
  {}

  if (exists)
    ...;


I prefer Charles' way because there is something in the catch
handler. Empty catch handler's are bad IMHO (and yes, there are *real*
reasons why that is so).



The reason I'm being so pugnacious about this is three fold:

1. I believe that it is dishonest to impose stylistic restrictions on
   use of computer languages 

2. I find it patronizing when people suggest that certain things make
   programs too difficult for support people or newbies or old people
   or people with glasses or short people or whatever

3. I've been implementing Scheme recently in GNU C and continuations
   are really cool and make you appreciate the whole role of the
   stack.


Nic


------------------------ Yahoo! Groups Sponsor --------------------~--> 
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

Before posting a question, try to find your answer here: 
<http://www.egroups.com/links/advanced-servlets>
Announcements should go to: [email protected]
To Post a message, send it to: [email protected]
To Unsubscribe, send a blank message to: [email protected] 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/advanced-servlets/

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/