logo

Java metiena izņēmums

Java programmā izņēmumi ļauj mums rakstīt labas kvalitātes kodus, kur kļūdas tiek pārbaudītas kompilēšanas laikā, nevis izpildlaikā, un mēs varam izveidot pielāgotus izņēmumus, kas atvieglo koda atkopšanu un atkļūdošanu.

Java mest atslēgvārdu

Java izmešanas atslēgvārds tiek izmantots, lai skaidri izmestu izņēmumu.

pārvērst strinu par int

Mēs precizējam izņēmums priekšmets, kas jāmet. Izņēmumam ir pievienots ziņojums, kas sniedz kļūdas aprakstu. Šie izņēmumi var būt saistīti ar lietotāja ievadi, serveri utt.

Mēs varam mest vai nu pārbaudītus, vai neatzīmētus izņēmumus Java, izmantojot atslēgvārdu. To galvenokārt izmanto, lai izmestu pielāgotu izņēmumu. Pielāgotus izņēmumus apspriedīsim vēlāk šajā sadaļā.

Mēs varam arī definēt savu nosacījumu kopu un skaidri norādīt izņēmumu, izmantojot atslēgvārdu throw. Piemēram, mēs varam izmantot Aritmētisko izņēmumu, ja mēs dalām skaitli ar citu skaitli. Šeit mums vienkārši jāiestata nosacījums un jāizmet izņēmums, izmantojot throw atslēgvārdu.

Java throw atslēgvārda sintakse ir norādīta zemāk.

mest instanci, t.i.,

 throw new exception_class('error message'); 

Apskatīsim IOException metiena piemēru.

 throw new IOException('sorry device error'); 

Kur Instancē ir jābūt Throwable tipam vai Throwable apakšklasei. Piemēram, Exception ir Throwable apakšklase, un lietotāja definētie izņēmumi parasti paplašina Exception klasi.

Java metiena atslēgvārda piemērs

1. piemērs: Neatzīmēta izņēmuma izmešana

Šajā piemērā mēs esam izveidojuši metodi validate(), kas pieņem veselu skaitli kā parametru. Ja vecums ir mazāks par 18, mēs izmantojam aritmētisko izņēmumu, pretējā gadījumā izdrukājiet ziņojumu, laipni lūdzam balsot.

TestThrow1.java

Šajā piemērā mēs esam izveidojuši validācijas metodi, kas kā parametru izmanto vesela skaitļa vērtību. Ja vecums ir mazāks par 18, mēs izmantojam aritmētisko izņēmumu, pretējā gadījumā izdrukājiet ziņojumu, laipni lūdzam balsot.

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

Izvade:

kas ir svn checkout
Java mest atslēgvārdu

3. piemērs: Lietotāja definēta izņēmuma izmešana

izņēmums ir viss pārējais Throwable klasē.

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()); } } } 

Izvade:

Java mest atslēgvārdu