constructors with exceptions
i have to create a class TornadoException with two constructors, one default, if the exception is thrown with the default constructor the method getMessage should return "Tornado: Take cover!"
the other constructor has a single parameter int m, if the exception is thrown with this constructor the getMessage should return "Tornado: m miles away!"
I also have to write a driver class that creates two objects, one with each constructor, and use a random number to choose which object to throw. and write a catch block that catches the exception and prints the string returned by the getMessage method.
this is a hw assignment and im not sure hw to approach it, it doesnt seem to be defined well, what am i supposed to put in the try block? any help would be great.
Thanks,
Peter
[816 byte] By [
pfishera] at [2007-11-27 6:23:33]

If your catch block is supposed to print out the c'tor's exception message, then the try block would be something like if (...) {
new WhateverException();
}
else {
new OtherException();
}
jverda at 2007-7-12 17:41:40 >

> creates two objects, one with each constructor,
Can you do this?
> use a random number to choose which object to throw.
Can you do this?
> write a catch block that catches the exception
Can you do this?
> prints the string returned by the getMessage method.
Can you do this?
Finally, if you can do all four, can you do them all, together?
Here is the TornadoException class
public class TornadoException
{
private String message;
public TornadoException()
{
message = "Tornado: Take cover immediately!";
}
public TornadoException(String str)
{
message = str;
}
public String getMessage()
{
return message;
}
}
Does this look right? I am unsure of how to create the driver class and how to correctly use the try catch blocks. Could anyone please help with a little more detail?
Thanks,
Peter
I am unsure of how to create
> the driver class and how to correctly use the try
> catch blocks. Could anyone please help with a little
> more detail?
> Thanks,
> Peter
You can use the class itself as a driver by adding a static main method:
public class TornadoException
{
public static void main(String[] args) {
// Throw and catch a properly initialized TornadoException here.
// How to do this should have been covered in
// class and in your textbook.
}
private String message;
public TornadoException()
{
message = "Tornado: Take cover immediately!";
}
public TornadoException(String str)
{
message = str;
}
public String getMessage()
{
return message;
}
}
Er, shouldn't you be deriving TornadoException from some class in the Throwable hierarchy? Say Exception or RuntimeException, although I'm still wrapping my head around tornadoes in the JVM.
heres my driver class so far,
public class TornadoDriver
{
public static void main(String[] args)
{
int i = 0;
i = (int) (Math.random() * 8);
try
{
if (i < 5)
throw new TornadoException();
else
throw new TornadoException(i);
}
catch (TornadoException e)
{
System.out.println(e.getMessage());
}
}
}
I am getting an error at complile incompatible types - found TornadoException expected java.lan. Throwable
How do I fix this problem, and any suggestions on my driver class?
Thanks,
Peter
Umm... see reply 5 for your issue here.
> How do I fix this problem, By reading my last reply -- #5.