Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Array of Exceptions

Reply
Thread Tools

Array of Exceptions

 
 
pascal@ameritech.net
Guest
Posts: n/a
 
      03-10-2007
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?

 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-10-2007
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).
 
Reply With Quote
 
 
 
 
Alan Cui
Guest
Posts: n/a
 
      03-10-2007
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).



 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      03-10-2007
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
 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      03-10-2007
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
}


 
Reply With Quote
 
pascal
Guest
Posts: n/a
 
      03-12-2007
> 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?

 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      03-12-2007
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.


 
Reply With Quote
 
pascal
Guest
Posts: n/a
 
      03-12-2007
// 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 ");
}
}

 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      03-13-2007
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
 
Reply With Quote
 
pascal
Guest
Posts: n/a
 
      03-14-2007
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 ");
}
}


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57