第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

從構(gòu)造函數(shù)拋出異常?

從構(gòu)造函數(shù)拋出異常?

慕斯709654 2023-10-27 10:51:34
public Section(Course course, String sectionNumber)        throws SectionException{try {/* No checking needed as a course is defined by another class. */this.thisCourse = course;this.sectionNumber = DEFAULT_SECTION_NUMBER;if( isValidSectionNumber(sectionNumber) )    this.sectionNumber = sectionNumber;} catch( ValidationException ex ) {    throw new SectionException("Error in constructor", ex);}}你好,這是我的代碼,如果這個(gè)構(gòu)造函數(shù)失敗,我需要拋出一個(gè)SectionException,但它不允許我這樣做,因?yàn)椤盁o(wú)法訪問(wèn)ValidationException的catch塊。這個(gè)異常永遠(yuǎn)不會(huì)從try語(yǔ)句主體中拋出”我該如何修復(fù)它?這是運(yùn)行良好的類似代碼public Student(String studentID, String firstName, String lastName)        throws StudentException{    /* Initialize with the provided data using the validated values. */    try    {        if( isValidStudentID(studentID) )        this.studentID = studentID;        if( isValidFirstName(firstName) )            this.firstName = firstName;        if( isValidLastName(lastName) )            this.lastName = lastName;    } catch( ValidationException ex )    {        throw new StudentException("Error in constructor", ex);    }}
查看完整描述

1 回答

?
猛跑小豬

TA貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊

您的 catch 塊無(wú)法訪問(wèn),因?yàn)?try 塊中沒(méi)有任何內(nèi)容拋出ValidationException. 要么手動(dòng)拋出此異常,例如:


if (isValidSectionNumber(sectionNumber))

    this.sectionNumber = sectionNumber;

else

    throw new ValidationException("Validation error: section number invalid");

或者讓你的捕獲接受一般錯(cuò)誤,例如


catch (Exception e) { /* other code here */ }

或者,您也可以從 if 條件中使用的方法之一拋出它。


我猜想在您提供的工作代碼中,一個(gè)或多個(gè)isValidStudentId(), isValidFirstName(),isValidLastName()會(huì)拋出一個(gè)ValidationExceptionwhere ,而在您的代碼中則不會(huì)。沒(méi)有看到這一切就無(wú)法判斷。


查看完整回答
反對(duì) 回復(fù) 2023-10-27
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)