(1)
class D61_OutOfRangeException extends Exception
{
}
public class Debug6_1 {
public static final int MAX_X = 1000;
public static final int MIN_X = 10;
private int x;
public int getX() {
return x;
}
public void setX( int value )throws D61_OutOfRangeException
{
if(value>=MIN_X&&value<=MAX_X)
{
x=value;
} else
{
throw new D61_OutOfRangeException();
}
}
public static void main( String[] args ) {
Debug6_1 a = new Debug6_1();
try
{
a.setX( 275 );
}
catch(D61_OutOfRangeException e)
{
}
System.out.println(a.getX());
}
}
(2)
interface IfaceDebug6_2 {
public void f( int input );
}
class D62_OutOfRangeException extends Exception {
}
public class Debug6_2 implements IfaceDebug6_2 {
public static final int MAX_X = 1000;
public static final int MIN_X = 10;
private int x;
public void f( int input )
{
try
{
setX( input );
}catch(D62_OutOfRangeException e)
{
}
}
public int getX() {
return x;
}
public void setX( int value )throws D62_OutOfRangeException
{
if ( value >= MIN_X && value <= MAX_X ) {
x = value;
} else {
throw new D62_OutOfRangeException();
}
}
public static void main( String[] args ) {
Debug6_2 a = new Debug6_2();
a.f( 275 );
System.out.println( a.getX() );
}
}
(3)
class D63_E1 extends Exception {
}
class D63_E2 extends D63_E1 {
}
public class Debug6_3
{
private int x;
public String toString() {
return String.valueOf( x );
}
public void f( int input ) throws D63_E1
{
x = input;
throw new D63_E1();
}
public static void main( String[] args )
{
Debug6_3 a = new Debug6_3();
try {
a.f( 275 );
} catch ( D63_E1 e ) {
System.out.println( e );
} catch (Exception e ) {
System.out.println( e );
}
System.out.println( a );
}
}
(4)
class TooBigException extends Exception
{
public double value;
public TooBigException( double errorValue ) {
value = errorValue;
}
public double getErrorValue() {
return value;
}
}
public class Debug6_4
{
private double pressure = 10.0;
public void setPressure( double newPressure )throws TooBigException
{
if ( newPressure > 2*pressure ) {
// pressure must be increased slowly
throw new TooBigException(newPressure);
} else {
pressure = newPressure;
}
}
public double getPressure() {
return pressure;
}
public static void main( String[] args ) {
try {
Debug6_4 x = new Debug6_4();
x.setPressure( 15.0 );
x.setPressure( 25.0 );
x.setPressure( 60.0 );
} catch ( TooBigException tbe )
{
System.out.println( tbe.value );
}
}
}
do it by yourself