Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Array of Exceptions (http://www.velocityreviews.com/forums/t483076-array-of-exceptions.html)

pascal@ameritech.net 03-10-2007 01:35 AM

Array of Exceptions
 
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 03-10-2007 02:07 AM

Re: Array of Exceptions
 
pascal@ameritech.net wrote:
> 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;

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 03-10-2007 02:33 AM

Re: Array of Exceptions
 
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.
> > Something like this:

>
> > public Object [] x = new Object [10];
> > Exception [] x = new Exception [10];

>
> > 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).




Tom Hawtin 03-10-2007 02:34 AM

Re: Array of Exceptions
 
pascal@ameritech.net wrote:
> 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 03-10-2007 03:12 AM

Re: Array of Exceptions
 
On Mar 9, 5:35 pm, pas...@ameritech.net wrote:
> 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?


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 03-12-2007 04:55 PM

Re: Array of Exceptions
 
> No, but you could do this:
>
> public interface ExceptionFactory<E extends Exception> {
> E createException();
>
> }
>
> public classIllegalMyExceptionFactoryimplements
> ExceptionFactory<IllegalMyException> {
> public IllegalMyException createException() {
> return new IllegalMyException();
> }
> }
>
> ExceptionFactory[] exceptionFactories = new ExceptionFactory[] {
> newIllegalMyExceptionFactory(); <--- Syntax error expecting }
> }
>
> public void y() throws Exception {
> 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 03-12-2007 06:47 PM

Re: Array of Exceptions
 
On Mar 12, 9:55 am, "pascal" <pas...@ameritech.net> wrote:
> > No, but you could do this:

>
> > public interface ExceptionFactory<E extends Exception> {
> > E createException();

>
> > }

>
> > public classIllegalMyExceptionFactoryimplements
> > ExceptionFactory<IllegalMyException> {
> > public IllegalMyException createException() {
> > return new IllegalMyException();
> > }
> > }

>
> > ExceptionFactory[] exceptionFactories = new ExceptionFactory[] {
> > newIllegalMyExceptionFactory(); <--- Syntax error expecting }
> > }

>
> > public void y() throws Exception {
> > 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?


Hmm, you somehow lost a space on that line, also you probably should
have the ; there.



pascal 03-12-2007 11:45 PM

Re: Array of Exceptions
 
// 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 03-13-2007 01:18 AM

Re: Array of Exceptions
 
pascal wrote:
> 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 03-14-2007 04:52 PM

Re: Array of Exceptions
 
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 ");
}
}




All times are GMT. The time now is 03:48 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.