一段简单的关于自定义异常抛出处理代码。
如下——
package cn.jsu.www11;
import java.util.Scanner;
class ComException extends Exception{
public ComException(){}
public ComException(String m) {
super(m);
}
}
class Fix{
public static int speed;
public static void fixcom() throws ComException{
if (speed <= 20)
throw new ComException("电脑严重卡慢,建议送修!");
else if (speed <= 40)
throw new ComException("电脑较为卡慢,建议清理内存!");
else if (speed <= 60)
throw new ComException("电脑有些卡顿,建议关闭不必要的后台程序!");
else
System.out.println("电脑运行速度为:" + speed);
System.out.println("电脑运行速度很快,打败了全国" + speed/10 +"9.99%的人!");
}
}
public class Computer {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
Fix.speed = s.nextInt();
try{
Fix.fixcom();
}catch (ComException e){
System.out.println("电脑运行速度为:" + Fix.speed);
System.out.println(e.getMessage());
}finally {
System.out.println("\n本次扫描检查结束,感谢使用!");
System.out.println("建议开通会员,享受每周自动检测清理!");
}
}
}