logo

Java metimo išimtis

Java programoje išimtys leidžia mums rašyti geros kokybės kodus, kai klaidos tikrinamos kompiliavimo metu, o ne vykdymo metu, ir galime sukurti pasirinktines išimtis, palengvinančias kodo atkūrimą ir derinimą.

Java mesti raktinį žodį

„Java mesti“ raktinis žodis naudojamas aiškiai išmesti išimtį.

kaip konvertuoti char į java eilutę

Mes nurodome išimtis objektas, kurį reikia mesti. Išimtyje yra tam tikras pranešimas, kuriame pateikiamas klaidos aprašymas. Šios išimtys gali būti susijusios su vartotojo įvestimis, serveriu ir kt.

Mes galime mesti pažymėtas arba nepažymėtas išimtis Java pagal mesti raktinį žodį. Jis daugiausia naudojamas pasirinktinai išimčiai išmesti. Tinkintas išimtis aptarsime vėliau šioje dalyje.

Taip pat galime apibrėžti savo sąlygų rinkinį ir aiškiai nurodyti išimtį naudodami raktinį žodį mesti. Pavyzdžiui, mes galime išmesti AritmetinęIšimtį, jei skaičių padalinsime iš kito skaičiaus. Čia tereikia nustatyti sąlygą ir mesti išimtį naudodami raktinį žodį mesti.

„Java mesti“ raktinio žodžio sintaksė pateikta žemiau.

mesti atvejį, t. y.

 throw new exception_class('error message'); 

Pažiūrėkime, kaip mesti IOException.

 throw new IOException('sorry device error'); 

Kai egzempliorius turi būti Throwable tipo arba Throwable poklasio. Pavyzdžiui, išimtis yra Throwable poklasis, o vartotojo nustatytos išimtys paprastai išplečia išimties klasę.

Java mesti raktinio žodžio pavyzdys

1 pavyzdys: Nepažymėtos išimties metimas

Šiame pavyzdyje sukūrėme metodą, pavadintą validate(), kuris priima sveikąjį skaičių kaip parametrą. Jei amžius yra jaunesnis nei 18, mes naudojame aritmetinę išimtį, kitaip atspausdinkite pranešimą, kviečiame balsuoti.

TestThrow1.java

Šiame pavyzdyje sukūrėme patvirtinimo metodą, kuris kaip parametrą priima sveikojo skaičiaus reikšmę. Jei amžius yra mažesnis nei 18, mes naudojame aritmetinę išimtį, priešingu atveju atspausdinkite pranešimą, kviečiame balsuoti.

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

Išvestis:

Java mesti raktinį žodį

3 pavyzdys: Vartotojo nustatytos išimties metimas

išimtis yra visa kita pagal Throwable klasę.

TestThrow3.java

„Java“ kartojimas
 // 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()); } } } 

Išvestis:

Java mesti raktinį žodį