Is is possble to create an array of exceptions.
Something like this:
public Object [] x = new Object [10];
Exception [] x = new Exception [10];
x [0] = IllegalMyException;
.
.
.
public void y () throws x[0]
{
throw new x[0];
}
try { y(); }
catch {x[0] e}
The above doesn't work. But is it because I have the syntax wrong, or
it is simply not possible?
Joshua Cranmer - 10 Mar 2007 02:07 GMT
> Is is possble to create an array of exceptions.
> Something like this:
[quoted text clipped - 3 lines]
>
> x [0] = IllegalMyException;
x[0] = new IllegalMyException();
> .
> .
> .
>
> public void y () throws x[0]
No. This is impossible: the throws clause must be determinable at
runtime, not at compile time.
> {
> throw new x[0];
throw x[0] would work.
> }
>
> try { y(); }
> catch {x[0] e}
See above comment for the throws clause.
> The above doesn't work. But is it because I have the syntax wrong, or
> it is simply not possible?
Strictly speaking, you can use arrays of Exceptions like any object.
However, you can not use them for what you're trying to use them. All
throws and catch must involve formal parameters (cf. JLS 1.3 §14.20 and
§8.4.6).
Alan Cui - 10 Mar 2007 02:33 GMT
Actually, I believe we could declare a "throws" clause with a
superclass of these arrayed exceptions. For simplicity, we could just
declare as "throws Exception".
On Mar 10, 10:07 am, Joshua Cranmer <Pidgeo...@epenguin.zzn.com>
wrote:
> pas...@ameritech.net wrote:
> > Is is possble to create an array of exceptions.
[quoted text clipped - 29 lines]
> throws and catch must involve formal parameters (cf. JLS 1.3 §14.20 and
> §8.4.6).
Tom Hawtin - 10 Mar 2007 02:34 GMT
> Exception [] x = new Exception [10];
>
> x [0] = IllegalMyException;
What is IllegalMyException? It doesn't look like an instanceof Exception.
> public void y () throws x[0]
You can use a fixed number of generic type parameters as exception types
in the exception part of a method declaration.
For instance:
interface Disposable<EXC extends Throwable> {
void dispose() throws EXC;
}
class SQLThing implements Disposable<java.sql.SQLException> {
...
public void dispose() throws java.sql.SQLException {
...
}
}
class NonThrowingThing implements Disposable<java.lang.RuntimeException> {
...
public void dispose() {
...
}
}
Did you have a particular use in mind?
Tom Hawtin
Daniel Pitts - 10 Mar 2007 03:12 GMT
On Mar 9, 5:35 pm, pas...@ameritech.net wrote:
> Is is possble to create an array of exceptions.
> Something like this:
[quoted text clipped - 18 lines]
> The above doesn't work. But is it because I have the syntax wrong, or
> it is simply not possible?
No, but you could do this:
public interface ExceptionFactory<E extends Exception> {
E createException();
}
public class IllegalMyExceptionFactory implements
ExceptionFactory<IllegalMyException> {
public IllegalMyException createException() {
return new IllegalMyException();
}
}
ExceptionFactory[] exceptionFactories = new ExceptionFactory[] {
new IllegalMyExceptionFactory();
}
public void y() throws Exception {
throw exceptionFactories[0].createException
}
pascal - 12 Mar 2007 17:55 GMT
> No, but you could do this:
>
[quoted text clipped - 17 lines]
> throw exceptionFactories[0].createException
> }
This almost worked. But I get a syntax error saying it expects a
closing brace.
Have you tried this code? Can you post a working version?
Daniel Pitts - 12 Mar 2007 19:47 GMT
> > No, but you could do this:
>
[quoted text clipped - 21 lines]
> closing brace.
> Have you tried this code? Can you post a working version?
Hmm, you somehow lost a space on that line, also you probably should
have the ; there.
pascal - 13 Mar 2007 00:45 GMT
// Here now is actually what I have.
public interface ExceptionFactory <E extends Exception>
{
E createException();
}
class IllegalMyExceptionFactory implements ExceptionFactory
<IllegalExceptionA>
{
public IllegalExceptionA createException()
{
return new IllegalExceptionA();
}
}
class MyProgram
{
ExceptionFactory [] exceptionFactories = new ExceptionFactory[]
{
new IllegalMyExceptionFactory()
};
public void y() throws Exception
{
throw exceptionFactories[0].createException();
}
public void x() throws Exception
{
throw exceptionFactories[1].createException ();
}
}
/*
Note I am trying to set up an array of lets say a dozen exceptions
that can be called
*in a manner that you show for method "y". But how do you initialise
the array to each
*one of these exception? I don't see how exceptionFactories[1] will
know
*anything about IllegalExceptionB.
*/
class IllegalExceptionA extends Exception
{
IllegalExceptionA ()
{
super ("IllegalExceptionA ");
}
}
class IllegalExceptionB extends Exception
{
IllegalExceptionB ()
{
super ("IllegalExceptionB ");
}
}
class IllegalExceptionC extends Exception
{
IllegalExceptionC ()
{
super ("IllegalExceptionC ");
}
}
Tom Hawtin - 13 Mar 2007 02:18 GMT
> public interface ExceptionFactory <E extends Exception>
> ExceptionFactory [] exceptionFactories = new ExceptionFactory[]
Arrays of generic types aren't pretty. These days there isn't a great
deal of point using arrays of references. Stick with Lists or other
collections, and it'll tend to be much simpler.
> public void y() throws Exception
> {
> throw exceptionFactories[0].createException();
> }
The type of exceptionFactories[0] is ExceptionFactory<? extends
Exception>. You could refine the code with say:
class MyProgram<T extends Exception> {
private final List<ExceptionFactory<T>> exceptionFactories...
public void y() throws T {
throw exceptionFactories.get(0).createException();
}
It's no different from a generic parameter used as a return or parameter
type. You can't say element number n of a collection has a particular
static type. Perhaps you are trying to use an array as a substitute for
introducing a new class.
Tom Hawtin
pascal - 14 Mar 2007 17:52 GMT
I got it to work...
Thanks to all, you provide the key ideas to get this to work.
You can cut and paste the following code into an IDE.
Hit the go button and the exception thrown depends on the array index
of the exception array.
Change the call to the method from x to y to z to see the effect.
I can now take these ideas and incorporate them into my program.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class MyProgram extends JFrame implements ActionListener
{
private JButton go = new JButton ("Go");
private Container pc = getContentPane();
Exception [] exceptionFactories = new Exception []
{
new IllegalExceptionA(),new IllegalExceptionB (), new
IllegalExceptionC ()
};
public void y() throws Exception
{
throw exceptionFactories[0];
}
public void x() throws Exception
{
throw exceptionFactories[1];
}
public void z () throws Exception
{
throw exceptionFactories [2];
}
public static void main (String [] args)
{
MyProgram p4 = new MyProgram ();
p4.setSize(640,480);
p4.setVisible(true);
p4.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
public MyProgram ()
{
pc.add (go);
go.addActionListener (this);
}
public void actionPerformed (ActionEvent e)
{
try
{
x();
}
catch (Exception exp)
{
System.out.println (exp.toString ());
}
}
}
class IllegalExceptionA extends Exception
{
IllegalExceptionA ()
{
super ("IllegalExceptionA ");
}
}
class IllegalExceptionB extends Exception
{
IllegalExceptionB ()
{
super ("IllegalExceptionB ");
}
}
class IllegalExceptionC extends Exception
{
IllegalExceptionC ()
{
super ("IllegalExceptionC ");
}
}
Lew - 15 Mar 2007 01:19 GMT
> I got it to work...
> Thanks to all, you provide the key ideas to get this to work.
[quoted text clipped - 3 lines]
> Change the call to the method from x to y to z to see the effect.
> I can now take these ideas and incorporate them into my program.
Bravo.
The approach I use is to declare one or two project-specific exceptions, one
checked and the other unchecked, e.g., FabulatorException and
FabulatorRuntimeException. Whenever the code catches a lower-level exception,
e.g., IOException, if I must rethrow it I rethrow the standard one with the
original one as the cause.
catch ( IOException ex )
{
String msg = "Some sensible log message with project-specific information. "
+ ex.getMessage();
logger.error( msg );
logger.debug( ex.getStackTrace() );
closeExternalResources();
throw new FabulatorException( msg, ex );
}
The times when I would rethrow a checked exception are few and far between in
any event.
catch ( IOException ex )
{
String msg = "Some sensible log message with project-specific information. "
+ ex.getMessage();
logger.error( msg );
logger.debug( ex.getStackTrace() );
closeExternalResources();
return ERROR;
}
The problems with the array approach are its verbosity and the reduction in
self-documentativity of the source. With only one custom Exception (or one
each of checked and unchecked), an array is completely superfluous anyhoo.
-- Lew
Daniel Pitts - 16 Mar 2007 00:02 GMT
> I got it to work...
> Thanks to all, you provide the key ideas to get this to work.
[quoted text clipped - 3 lines]
> Change the call to the method from x to y to z to see the effect.
> I can now take these ideas and incorporate them into my program.
Oh, you weren't just asking a theory question?
I would then ask WHY you are doing this? The design looks error prone
and unmaintainable. What reasons do you have to use this
(anti-)pattern?