In my method VoteTest I would like to have a case that throws an exception
with some information to the user if a is a number different from 1 or 2 ( a
= = 1 || a = =2). Is that possible?
public class VoteTest {
private int knud;
private int jesper;
public void test(int a){
switch(a)
{
case 1:
knud = knud + 1;
break;
case 2:
jesper = jesper + 1;
break;
}
}
Eric Sosman - 19 Jan 2005 22:59 GMT
> In my method VoteTest I would like to have a case that throws an exception
> with some information to the user if a is a number different from 1 or 2 ( a
[quoted text clipped - 6 lines]
>
> public void test(int a)
throws StupidVoterException
> {
> switch(a)
[quoted text clipped - 5 lines]
> jesper = jesper + 1;
> break;
default:
throw new StupidVoterException(
"Why waste your vote on " + a + "?");
> }
> }
This assumes that the StupidVoterException class (or whatever
other Exception you decide to use) has a constructor that takes
a String argument. Most (perhaps all) Exceptions have such a
constructor, and any that don't ought to (see the Javadoc for
Throwable).

Signature
Eric.Sosman@sun.com
Alex Kizub - 19 Jan 2005 23:58 GMT
> In my method VoteTest I would like to have a case that throws an exception
> with some information to the user if a is a number different from 1 or 2 ( a
> = = 1 || a = =2). Is that possible?
public class VoteTest {
private int knud;
private int jesper;
public static void main (String []a) {
VoteTest t=new VoteTest();
t.test(1);
t.test(2);
t.test(3);
}
public void test(int a){
switch(a)
{
case 1:
knud = knud + 1;
break;
case 2:
jesper = jesper + 1;
break;
default: throw new RuntimeException("Error: "+a+"is not 1 or 2!");
}
}
}
Alex Kizub.