我想要做的是給2至60之間的數字。如果數字不包括在這個「空間」,那麼將有一個例外,名稱InvalidLicenceNumberException
。我有和類UserInput,但它並不需要上傳其確定 我的主:例外如何使用,如果在嘗試趕上
import java.io.*;
public class Exercv {
public static void main(String[] args) {
int ArTh ;
ArTh=Vehicle.NumberPos();
try {
if(ArTh<2 || ArTh>60){
ArThEx();
}
} catch(InvalidLicenceNumberException e){
System.out.println(""+e);
throw new InvalidLicenceNumberException ("There is an exception!");
}
}
我的類車輛:
public abstract class Vehicle {
static int user; //for userinput
public static int ArTh() {
System.out.println("Give number between 2 until 60");
user=UserInput.getInteger();
if(user>=2 && user<=60){
System.out.println(" Possition is : "+ user);
}
return user;
}
public static void ArThEx() throws ArithmeticException{
System.err.println("There is an exception");
System.err.println("Positions arent acceptable");
}
}
我的特例:
import java.lang.Exception;
public class InvalidLicenceNumberExceptio extends MyException {
public InvalidLicenceNumberExceptio(String s){
super(s);
}
}
請格式化您的代碼。 – SomeJavaGuy
是的,我看到我沒有做得很好對不起。我會 –
這可能是有聯繫的,並且很有用:[我如何創建一個異常,以遏制int範圍等...](http://stackoverflow.com/questions/15468418/how-can-i-create-an-exception-that-checks-for-int-range-number-type-and-not-a) –