java異常類(lèi)的完整代碼 java異常大全

java中異常類(lèi)的實(shí)例

這里寫(xiě)了

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),泰興企業(yè)網(wǎng)站建設(shè),泰興品牌網(wǎng)站建設(shè),網(wǎng)站定制,泰興網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,泰興網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專(zhuān)業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

if(length=0||width=0)

{

throw?new?NoValueException("數(shù)值非法");

}

java異常求代碼

public class Student {

private Integer age;

private Integer sex;

public Integer getAge() {

return age;

}

public void setAge(Integer age) throws Exception{

if(age 0 || age 100)

throw new Exception("年齡不在合理范圍內(nèi)");

this.age = age;

}

public Integer getSex() {

return sex;

}

public void setSex(Integer sex) throws Exception{

if(!(sex == 0 sex == 1))

throw new Exception("性別不是男女");

this.sex = sex;

}

public Student(Integer age, Integer sex) throws Exception{

super();

if(age 0 || age 100)

throw new Exception("年齡不在合理范圍內(nèi)");

if(!(sex == 0 sex == 1))

throw new Exception("性別不是男女");

this.age = age;

this.sex = sex;

}

public Student() {

super();

}

public static void main(String[] args) {

try {

Student student = new Student(101,2);

} catch (Exception e) {

System.out.println(e.getMessage());

e.printStackTrace();

}

}

}

Java異常類(lèi)

package?mp3;

/**

*?歌曲類(lèi)?。這里重寫(xiě)equals方法定義一首歌(不是hash結(jié)構(gòu),所以不用重寫(xiě)hashCode())

*/

public?class?Mp3?{

//?歌曲名稱(chēng)

private?String?name;

//?歌曲時(shí)間

private?double?time;

public?String?getName()?{

return?name;

}

public?void?setName(String?name)?{

this.name?=?name;

}

public?double?getTime()?{

return?time;

}

public?void?setTime(double?time)?{

this.time?=?time;

}

@Override

public?boolean?equals(Object?obj)?{

if?(this?==?obj)

return?true;

if?(obj?==?null)

return?false;

if?(getClass()?!=?obj.getClass())

return?false;

Mp3?other?=?(Mp3)?obj;

if?(name?==?null)?{

if?(other.name?!=?null)

return?false;

}?else?if?(!name.equals(other.name))

return?false;

if?(Double.doubleToLongBits(time)?!=?Double.doubleToLongBits(other.time))

return?false;

return?true;

}

public?Mp3(String?name,?double?time)?{

super();

this.name?=?name;

this.time?=?time;

}

}

package?mp3;

import?java.util.List;

/**

*?播放器類(lèi)

*?構(gòu)造器有三個(gè)?:?1、無(wú)歌曲播放器。2、單曲播放器。3、列表播放器

*?提供三個(gè)方法?:?1、播放單曲。2、播放下一曲。3、播放上一曲

*/

public?class?PlayTool?{

//?播放列表

ListMp3?mp3Lst;

//?當(dāng)前播放Mp3

private?Mp3?currPlaymp3;

//?列表播放

public?PlayTool(ListMp3?mp3Lst)?{

super();

this.mp3Lst?=?mp3Lst;

}

//?創(chuàng)建無(wú)曲目構(gòu)造器

public?PlayTool()?{

super();

}

//?單曲播放

public?PlayTool(Mp3?currPlaymp3)?{

super();

this.currPlaymp3?=?currPlaymp3;

}

public?Mp3?getCurrPlaymp3()?{

return?currPlaymp3;

}

public?void?setCurrPlaymp3(Mp3?currPlaymp3)?{

this.currPlaymp3?=?currPlaymp3;

}

public?ListMp3?getMp3Lst()?{

return?mp3Lst;

}

public?void?setMp3Lst(ListMp3?mp3Lst)?{

this.mp3Lst?=?mp3Lst;

}

/**

?*?播放曲目

?*?

?*?@param?mp3

?*/

public?void?play(Mp3?mp3)throws?Exception{

this.setCurrPlaymp3(mp3);

System.out.println("正在播放歌曲"?+?mp3.getName());

//加入睡眠,簡(jiǎn)單模擬播放時(shí)長(zhǎng)

Thread.sleep((long)?(mp3.getTime()*500));

}

/**

?*?播放曲目

?*?

?*?@param?mp3

?*/

public?void?play()throws?Exception{

if?(mp3Lst?==?null?||?mp3Lst.size()?==?0)?{

throw?new?Mp3Exception("無(wú)法獲取歌單列表");

}else{

play(mp3Lst.get(0));

}

}

/**

?*?播放下一首

?*/

public?void?playNext()?throws?Exception?{

if?(mp3Lst?==?null?||?mp3Lst.size()?==?0)?{

throw?new?Mp3Exception("無(wú)法獲取歌單列表");

}?else?{

/**?如果當(dāng)前沒(méi)有正在播放的歌曲,那么默認(rèn)播放列表中第一首?*/

if?(currPlaymp3?==?null)?{

play(mp3Lst.get(0));

}?else?{

int?indexOf?=?mp3Lst.indexOf(currPlaymp3);

indexOf++;

if?(indexOf?==?mp3Lst.size())?{

throw?new?Mp3Exception("已經(jīng)是最后一首歌曲,沒(méi)有下一首");

}?else?{

play(mp3Lst.get(indexOf));

}

}

}

}

/**

?*?播放上一首

?*/

public?void?playLast()?throws?Exception?{

if?(mp3Lst?==?null?||?mp3Lst.size()?==?0)?{

throw?new?Mp3Exception("無(wú)法獲取歌單列表");

}?else?{

/**?如果當(dāng)前沒(méi)有正在播放的歌曲,那么默認(rèn)播放列表中第一首?*/

if?(currPlaymp3?==?null)?{

play(mp3Lst.get(0));

}?else?{

int?indexOf?=?mp3Lst.indexOf(currPlaymp3);

indexOf--;

if?(indexOf??0)?{

throw?new?Mp3Exception("歌單中第一首歌曲,沒(méi)有上一首");

}?else?{

play(mp3Lst.get(indexOf));

}

}

}

}

}

package?mp3;

/**

*?自定義Mp3Exception

*/

public?class?Mp3Exception?extends?Exception?{

public?Mp3Exception()?{

super();

}

public?Mp3Exception(String?message,?Throwable?cause)?{

super(message,?cause);

}

public?Mp3Exception(Throwable?cause)?{

super(cause);

}

public?Mp3Exception(String?message)?{

super(message);

}

}

package?mp3;

import?java.util.ArrayList;

import?java.util.List;

/**

*?測(cè)試類(lèi)

*?@author?remind

*

*/

public?class?Test?{

//歌單列表

static?ListMp3?list?=?new?ArrayListMp3();

//靜態(tài)代碼塊填充歌曲

static{

list.add(new?Mp3("小步舞曲-陳綺貞",3.53));

list.add(new?Mp3("城市稻草人-曹芳",2));

list.add(new?Mp3("再見(jiàn)-孝琳",3));

list.add(new?Mp3("櫻花飛舞時(shí)-中島美嘉",4.2));

}

public?static?void?main(String[]?args)?{

try?{

/**

?*?正常播放

?*/

/*創(chuàng)建播放器*/

PlayTool?playTool?=?new?PlayTool(list);

//開(kāi)始播放歌曲

playTool.play(new?Mp3("小步舞曲-陳綺貞",3.53));

//播放下一曲

playTool.playNext();

/**

?*?異常播放,無(wú)上一曲

?*/

playTool.play(new?Mp3("小步舞曲-陳綺貞",3.53));

playTool.playLast();

/**

?*?異常播放,無(wú)下一曲

?*/

playTool.play(new?Mp3("櫻花飛舞時(shí)-中島美嘉",4.2));

playTool.playNext();

}?catch?(Exception?e)?{

e.printStackTrace();

}

}

}

java異常處理的問(wèn)題:下面是代碼:

main()方法里的輸出語(yǔ)句是一定會(huì)執(zhí)行的。

捕捉到異常就會(huì)執(zhí)行catch。

FileReader f1=new FileReader("D:\\mytest.txt");報(bào)錯(cuò)是因?yàn)镕ileReader類(lèi)的夠著方法是這樣聲明的public FileReader(File file) throws FileNotFoundException必須捕獲異常或者聲明拋出異常

如何用Java程序來(lái)編寫(xiě)一個(gè)異常?

class

MyException

extends

Exception

//自定義的異常類(lèi)

繼承Exception類(lèi)

{

private

String

exceptionName;

//定義一個(gè)私有變量,用來(lái)為自定義異常

public

MyException(){}

//創(chuàng)建一個(gè)無(wú)參數(shù)的構(gòu)造函數(shù)

public

MyException(String

exceptionName){

//創(chuàng)建一個(gè)有參數(shù)的構(gòu)造函數(shù),傳入的參數(shù)為前面定義的異常名稱(chēng)

this.exceptionName=exceptionName;

}

public

String

getExceptionName(){

//定義一個(gè)方法,提供給外部來(lái)獲取私有變量

return

this.exceptionName;

}

public

static

void

main(String

[]

args){

try{

System.out.println("自定義的異常類(lèi)對(duì)象");

throw

new

MyException("自定義的異常");//拋一個(gè)自定義的異常類(lèi)對(duì)象,傳入的參數(shù)就是給控制臺(tái)看的異常

}catch(MyException

e){

System.out.println("異常信息:"+e.getExceptionName());

}

}

}

我已經(jīng)盡力你……你懂的!

關(guān)于java自定義異常類(lèi)的一個(gè)代碼

1 程序中的異常指不期而至的各種狀況,如:文件找不到、網(wǎng)絡(luò)連接失敗、非法參數(shù)等。異常是一個(gè)事件,它發(fā)生在程序運(yùn)行期間,干擾了正常的指令流程。Java通 過(guò)API中Throwable類(lèi)的眾多子類(lèi)描述各種不同的異常。因而,Java異常都是對(duì)象,是Throwable子類(lèi)的實(shí)例,描述了出現(xiàn)在一段編碼中的 錯(cuò)誤條件。當(dāng)條件生成時(shí),錯(cuò)誤將引發(fā)異常。

Java異常類(lèi)層次結(jié)構(gòu)圖:

使用Java內(nèi)置的異常類(lèi)可以描述在編程時(shí)出現(xiàn)的大部分異常情況。除此之外,用戶還可以自定義異常。用戶自定義異常類(lèi),只需繼承Exception類(lèi)即可。

在程序中使用自定義異常類(lèi),大體可分為以下幾個(gè)步驟。

(1)創(chuàng)建自定義異常類(lèi)。

(2)在方法中通過(guò)throw關(guān)鍵字拋出異常對(duì)象。

(3)如果在當(dāng)前拋出異常的方法中處理異常,可以使用try-catch語(yǔ)句捕獲并處理;否則在方法的聲明處通過(guò)throws關(guān)鍵字指明要拋出給方法調(diào)用者的異常,繼續(xù)進(jìn)行下一步操作。

(4)在出現(xiàn)異常方法的調(diào)用者中捕獲并處理異常。

創(chuàng)建自定義異常類(lèi)

public?class?MyException?extends?Exception?{

private?static?final?long?serialVersionUID?=?1L;

public?MyException(){

super();

}

public?MyException(String?msg){

super(msg);

}

}

使用的話就不演示了 如果你已經(jīng)研究到了自定義異常 那么我相信你也一定會(huì)使用了?

如果不會(huì)使用 建議學(xué)會(huì)使用后再來(lái)看這篇文章

新聞名稱(chēng):java異常類(lèi)的完整代碼 java異常大全
本文鏈接:http://muchs.cn/article46/dosdieg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、虛擬主機(jī)、App開(kāi)發(fā)、網(wǎng)站制作、用戶體驗(yàn)、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化