Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Class Geshiexception extends exception{
Public Geshiexception (String a) {
Super (a);
}
}
public class Score {
public static void Main (string[] args) throws ioexception{
BufferedReader cin=new BufferedReader (New InputStreamReader (system.in)); // input function
System.out.println (" Please enter your score:");
String Str=cin.readline ();
int t=0;
Double A;
while (true) { //until the input result is formatted correctly before stopping
try{ // monitor the program that is prone to problems
for (int i=0;i<str.length (); i++)
{
if (Str.charat (i) > ' 9 ' | | Str.charat (i) < ' 0 ')
{
throw new Geshiexception (" Malformed! ");
}
}
A=double.parsedouble (str); To turn a string type to a double type
if (a>100)
throw new Geshiexception (" Malformed! Value ");
Break
}
catch (Geshiexception e) { // exception handling
System.out.println (e);
System.out.println (" please re-enter the score:");
Str=cin.readline ();
}
}
if (a>=90)
System.out.println (" excellent ");
else if (a<90&&a>=80)
System.out.println (" Liang ");
else if (a<80&&a>70)
System.out.println (" medium ");
else if (a<70&&a>60)
System.out.println (" pass ");
else if (a<60)
System.out.println (" less than lattice ");
}
}
Run
JAVA Exception Handling