ใน Java มีวิธีใดบ้างในการรับ (catch) ทั้งหมดexceptionsแทนที่จะจับข้อยกเว้นทีละรายการ?
ใน Java มีวิธีใดบ้างในการรับ (catch) ทั้งหมดexceptionsแทนที่จะจับข้อยกเว้นทีละรายการ?
คำตอบ:
หากคุณต้องการคุณสามารถเพิ่มประโยคพ่นในวิธีการของคุณ จากนั้นคุณไม่จำเป็นต้องตรวจสอบวิธีการทันที ด้วยวิธีนี้คุณสามารถจับได้ในexceptionsภายหลัง (อาจจะเป็นเวลาเดียวกันกับที่อื่น ๆexceptions)
รหัสมีลักษณะดังนี้:
public void someMethode() throws SomeCheckedException {
    //  code
}
จากนั้นคุณสามารถจัดการกับไฟล์ exceptionsกรณีที่คุณไม่ต้องการจัดการกับพวกเขาด้วยวิธีนั้น
ในการตรวจจับข้อยกเว้นทั้งหมดของโค้ดบางบล็อกอาจทำให้คุณทำได้: (สิ่งนี้จะจับExceptionsได้ว่าคุณเขียนเองด้วย)
try {
    // exceptional block of code ...
    // ...
} catch (Exception e){
    // Deal with e as you please.
    //e may be any type of exception at all.
}
เหตุผลที่ใช้งานได้เนื่องจากExceptionเป็นคลาสฐานสำหรับข้อยกเว้นทั้งหมด ดังนั้นข้อยกเว้นใด ๆ ที่อาจถูกโยนทิ้งคือException(ตัวพิมพ์ใหญ่ 'E')
หากคุณต้องการจัดการข้อยกเว้นของคุณเองก่อนอื่นให้เพิ่มcatchบล็อกก่อนข้อยกเว้นทั่วไป
try{    
}catch(MyOwnException me){
}catch(Exception e){
}
    แม้ว่าฉันจะยอมรับว่ามันไม่ใช่รูปแบบที่ดีในการจับข้อยกเว้นแบบดิบ แต่ก็มีวิธีจัดการข้อยกเว้นที่ให้การบันทึกที่เหนือกว่าและความสามารถในการจัดการกับสิ่งที่ไม่คาดคิด เนื่องจากคุณอยู่ในสถานะพิเศษคุณอาจสนใจที่จะได้รับข้อมูลที่ดีมากกว่าเวลาตอบสนองดังนั้นการแสดงประสิทธิภาพจึงไม่ควรเป็นที่นิยม
try{
    // IO code
} catch (Exception e){
    if(e instanceof IOException){
        // handle this exception type
    } else if (e instanceof AnotherExceptionType){
        //handle this one
    } else {
        // We didn't expect this one. What could it be? Let's log it, and let it bubble up the hierarchy.
        throw e;
    }
}
อย่างไรก็ตามสิ่งนี้ไม่ได้คำนึงถึงข้อเท็จจริงที่ว่า IO สามารถโยนข้อผิดพลาดได้เช่นกัน ข้อผิดพลาดไม่ใช่ข้อยกเว้น ข้อผิดพลาดอยู่ภายใต้ลำดับชั้นการสืบทอดที่แตกต่างจากข้อยกเว้นแม้ว่าทั้งสองจะแชร์คลาสพื้นฐานที่สามารถโยนได้ เนื่องจาก IO สามารถโยนข้อผิดพลาดได้คุณอาจต้องการไปให้ไกลที่สุดเพื่อจับ Throwable
try{
    // IO code
} catch (Throwable t){
    if(t instanceof Exception){
        if(t instanceof IOException){
            // handle this exception type
        } else if (t instanceof AnotherExceptionType){
            //handle this one
        } else {
            // We didn't expect this Exception. What could it be? Let's log it, and let it bubble up the hierarchy.
        }
    } else if (t instanceof Error){
        if(t instanceof IOError){
            // handle this Error
        } else if (t instanceof AnotherError){
            //handle different Error
        } else {
            // We didn't expect this Error. What could it be? Let's log it, and let it bubble up the hierarchy.
        }
    } else {
        // This should never be reached, unless you have subclassed Throwable for your own purposes.
        throw t;
    }
}
    จับข้อยกเว้นพื้นฐาน 'Exception'
   try { 
         //some code
   } catch (Exception e) {
        //catches exception and all subclasses 
   }
    เป็นการปฏิบัติที่ไม่ดีในการจับข้อยกเว้น - มันกว้างเกินไปและคุณอาจพลาดบางอย่างเช่นNullPointerExceptionในโค้ดของคุณเอง
สำหรับการทำงานของไฟล์ส่วนใหญ่IOExceptionเป็นข้อยกเว้นของรูท ดีกว่าที่จะจับสิ่งนั้นแทน
ใช่มี.
try
{
    //Read/write file
}catch(Exception ex)
{
    //catches all exceptions extended from Exception (which is everything)
}
    คุณอาจพบข้อยกเว้นหลายข้อในบล็อกเดียว
try{
  // somecode throwing multiple exceptions;
} catch (Exception1 | Exception2 | Exception3 exception){
  // handle exception.
} 
    คุณหมายถึงจับประเภทExceptionใดก็ได้ที่ถูกโยนซึ่งตรงข้ามกับข้อยกเว้นเฉพาะหรือไม่?
ถ้าเป็นเช่นนั้น:
try {
   //...file IO...
} catch(Exception e) {
   //...do stuff with e, such as check its type or log it...
}