Exception中有一個特殊的子類異常RuntimeException運行時異常。
如果在函數內拋出該異常,函數上可以不用聲明,編譯一樣通過。
如果在函數上聲明了該異常。調用者可以不用進行處理。編譯一樣通過。
之所以不用在函數上聲明,是因為不需要讓調用者處理。
當該異常發生,希望程序停止,因為在運行時,出現了無法繼續運算的情況,希望停止程序后,對代碼進行修正。
自定義異常時:如果該異常的發生,無法再繼續進行運算,就讓自定義異常繼承RuntimeException.
對于異常分兩種:
1、編譯時被監測的異常
2、編譯時不被監測的異常(運行時異常,RuntimeException以及其子類)
class FuShuException extends RuntimeException {
FuShuException(String msg)
{
super(msg);
}
}
class Demo {
int div(int a,int b)//函數上沒有拋出異常,因為FuShuException是RuntimeException的子類
{
if(b<0)
throw new FuShuException("除數為負數");
if(b==0)
throw new ArithmeticException("被0除了");
return a/b;
}
}
class ExceptionDemo7 {
public static void main(String[] args)
{
Demo d = new Demo();
int x = d.div(4,-4);
System.out.println("x="+x);
}
}
輸出結果:
Exception in thread "main" FuShuException: 除數為負數
at Demo.div(ExceptionDemo00.java:15)
at ExceptionDemo00.main(ExceptionDemo00.java:28)
舉例:
問題是:
電腦冒煙
電腦藍屏
要對問題進行描述,封裝成對象。
可是當冒煙發生后,出現講課進度無法繼續。
出現了老師的問題:課時計劃無法完成
class LanPingException extends Exception {
LanPingException(String message)
{
super(message);
}
}
class MaoYanException extends Exception {
MaoYanException(String message)
{
super(message);
}
}
class NoPlanException extends Exception {
NoPlanException(String msg)
{
super(msg);
}
}
class Computer {
private int state = 3;
public void run()throws LanPingException,MaoYanException
{
if(state==2)
throw new LanPingException("藍屏了");
if(state==3)
throw new MaoYanException("冒煙了");
System.out.println("電腦運行");
}
public void reset()
{
state = 1;
System.out.println("電腦重啟");
}
}
class Teacher {
private String name;
private Computer cmpt;
Teacher(String name)
{
this.name = name;
cmpt = new Computer();
}
public void prelect()throws NoPlanException
{
try
{
cmpt.run();
}
catch(LanPingException e)
{
cmpt.reset();
}
catch(MaoYanException e)
{
test();
throw new NoPlanException("課時無法繼續"+e.getMessage());
}
System.out.println("講課了!");
}
public void test()
{
System.out.println("LianXi");
}
}
class ExceptionTest {
public static void main(String[] args)
{
Teacher t = new Teacher("Wang");
try
{
t.prelect();
}
catch(NoPlanException e)
{
System.out.println(e.toString());
System.out.println("換老師或者放假");
}
}
}
運行結果:
LianXi
NoPlanException: 課時無法繼續冒煙了
換老師或者放假
舉例:
有一個圓形和長方形。都可以獲取面積。
對于面積如果出現非法的數值,視為是獲取面積出現問題。問題通過異常來表示。現在對這個程序進行基本設計。
//定義一個異常,讓它繼承運行時異常
class NoValueException extends RuntimeException {
NoValueException(String msg)
{
super(msg);
}
}
//定義一個接口
interface Shape {
void getArea();
}
//長方形實現Shape接口
class Rectangle implements Shape {
private int len,wid;
Rectangle(int len,int wid)//這里不用throws NoValueException,因為NoValueException是RuntimeException的子類
{
if(len<=0||wid<=0)
throw new NoValueException("出現非法值");
this.len = len;
this.wid = wid;
}
public void getArea()
{
System.out.println(len*wid);
}
}
class Circle implements Shape {
private double radius;
public static final double PI = 3.14;
Circle(double radius)
{
if(radius<=0)
throw new NoValueException("非法");
this.radius = radius;
}
public void getArea()
{
System.out.println(PI*radius*radius);
}
}
class Demo2
{
public static void main(String[] args)
{
Rectangle r = new Rectangle(3,4);
r.getArea();
Circle circle = new Circle(-8);
circle.getArea();
System.out.println("over");
}
}