logo

جافا رمي استثناء

في Java، تسمح لنا الاستثناءات بكتابة رموز عالية الجودة حيث يتم فحص الأخطاء في وقت الترجمة بدلاً من وقت التشغيل ويمكننا إنشاء استثناءات مخصصة مما يجعل استرداد التعليمات البرمجية وتصحيح الأخطاء أسهل.

جافا رمي الكلمة الأساسية

يتم استخدام الكلمة الأساسية Java throw لطرح استثناء بشكل صريح.

معرفات صالحة في جافا

نحن نحدد استثناء الكائن الذي سيتم رميه. يحتوي الاستثناء على بعض الرسائل التي توفر وصف الخطأ. قد تكون هذه الاستثناءات مرتبطة بإدخالات المستخدم والخادم وما إلى ذلك.

يمكننا طرح استثناءات محددة أو غير محددة في Java عن طريق رمي الكلمة الأساسية. يتم استخدامه بشكل أساسي لطرح استثناء مخصص. سنناقش الاستثناءات المخصصة لاحقًا في هذا القسم.

يمكننا أيضًا تحديد مجموعة الشروط الخاصة بنا وطرح استثناء بشكل صريح باستخدام الكلمة الأساسية throw. على سبيل المثال، يمكننا طرح استثناء ArithmeticException إذا قسمنا رقمًا على رقم آخر. هنا، نحتاج فقط إلى ضبط الشرط وطرح الاستثناء باستخدام الكلمة الأساسية throw.

بناء جملة الكلمة الأساسية لرمي Java موضح أدناه.

رمي مثيل أي،

 throw new exception_class('error message'); 

دعونا نرى مثال رمي IOException.

 throw new IOException('sorry device error'); 

حيث يجب أن يكون المثيل من النوع Throwable أو فئة فرعية من Throwable. على سبيل المثال، الاستثناء هو فئة فرعية من Throwable والاستثناءات المعرفة من قبل المستخدم عادةً ما تمتد إلى فئة الاستثناء.

جافا رمي مثال على الكلمات الرئيسية

المثال 1: طرح استثناء غير محدد

في هذا المثال، قمنا بإنشاء طريقة باسم validate() تقبل عددًا صحيحًا كمعلمة. إذا كان العمر أقل من 18 عامًا، فسنقوم بطرح ArithmeticException وإلا قم بطباعة رسالة ترحيب بالتصويت.

TestThrow1.java

في هذا المثال، قمنا بإنشاء طريقة التحقق من الصحة التي تأخذ قيمة عدد صحيح كمعلمة. إذا كان العمر أقل من 18 عامًا، فسنقوم بطرح ArithmeticException وإلا قم بطباعة رسالة ترحيب بالتصويت.

 public class TestThrow1 { //function to check if person is eligible to vote or not public static void validate(int age) { if(age<18) { throw arithmetic exception if not eligible to vote new arithmeticexception('person is vote'); } else system.out.println('person vote!!'); main method public static void main(string args[]){ calling the function validate(13); system.out.println('rest of code...'); < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/exception-handling/63/java-throw-exception.webp" alt="Java throw keyword"> <p>The above code throw an unchecked exception. Similarly, we can also throw unchecked and user defined exceptions.</p> <h4>Note: If we throw unchecked exception from a method, it is must to handle the exception or declare in throws clause.</h4> <p>If we throw a checked exception using throw keyword, it is must to handle the exception using catch block or the method must declare it using throws declaration.</p> <h3>Example 2: Throwing Checked Exception</h3> <h4>Note: Every subclass of Error and RuntimeException is an unchecked exception in Java. A checked exception is everything else under the Throwable class.</h4> <p> <strong>TestThrow2.java</strong> </p> <pre> import java.io.*; public class TestThrow2 { //function to check if person is eligible to vote or not public static void method() throws FileNotFoundException { FileReader file = new FileReader(&apos;C:\Users\Anurati\Desktop\abc.txt&apos;); BufferedReader fileInput = new BufferedReader(file); throw new FileNotFoundException(); } //main method public static void main(String args[]){ try { method(); } catch (FileNotFoundException e) { e.printStackTrace(); } System.out.println(&apos;rest of the code...&apos;); } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/exception-handling/63/java-throw-exception-2.webp" alt="Java throw keyword"> <h3>Example 3: Throwing User-defined Exception</h3> exception is everything else under the Throwable class. <p> <strong>TestThrow3.java</strong> </p> <pre> // class represents user-defined exception class UserDefinedException extends Exception { public UserDefinedException(String str) { // Calling constructor of parent Exception super(str); } } // Class that uses above MyException public class TestThrow3 { public static void main(String args[]) { try { // throw an object of user defined exception throw new UserDefinedException(&apos;This is user-defined exception&apos;); } catch (UserDefinedException ude) { System.out.println(&apos;Caught the exception&apos;); // Print the message from MyException object System.out.println(ude.getMessage()); } } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/exception-handling/63/java-throw-exception-3.webp" alt="Java throw keyword"> <hr></18)>

انتاج:

جافا رمي الكلمة الأساسية

المثال 3: طرح استثناء محدد من قبل المستخدم

الاستثناء هو كل شيء آخر ضمن فئة Throwable.

TestThrow3.java

حدود المغلق
 // class represents user-defined exception class UserDefinedException extends Exception { public UserDefinedException(String str) { // Calling constructor of parent Exception super(str); } } // Class that uses above MyException public class TestThrow3 { public static void main(String args[]) { try { // throw an object of user defined exception throw new UserDefinedException(&apos;This is user-defined exception&apos;); } catch (UserDefinedException ude) { System.out.println(&apos;Caught the exception&apos;); // Print the message from MyException object System.out.println(ude.getMessage()); } } } 

انتاج:

جافا رمي الكلمة الأساسية