日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java运行时异常中文_JAVA——运行时异常(RuntimeException)

發布時間:2023/12/9 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java运行时异常中文_JAVA——运行时异常(RuntimeException) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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");

}

}

總結

以上是生活随笔為你收集整理的java运行时异常中文_JAVA——运行时异常(RuntimeException)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。