An výjimka je nechtěná a neočekávaná chyba v programu. Ve většině případů nastane výjimka, když je v našem kódu chyba, ale lze ji zvládnout. Narušuje normální tok kódu.
Kód například vyvolá výjimku, pokud uživatel zadal neplatné informace, pokud kód není schopen přečíst soubor umístěný na vzdáleném místě, nebo pokud dojde ke ztrátě síťového připojení uprostřed komunikace.
IllegalStateException v Javě
IllegalStateException je podtřídou třídy RuntimeException, a proto se jedná o nekontrolovanou výjimku. Explicitně ji vyvolává programátor nebo vývojář API. Je vyvoláno, když je volání metody nelegální nebo je metoda volána v nesprávný čas.
schéma modelu e-r
Například, jakmile spustíme vlákno, nemůžeme znovu spustit stejné vlákno; pokud se o to pokusíme, vyvolá výjimku za běhu, tj. IllegalStateException .
Výjimka smět vznikají v kódu obvykle, když pracujeme s frameworkem Collections. Seznam, Fronta, Mapy, Strom jsou některé ze sbírek. Z nich mají seznam a fronty tendenci házet výjimku nelegálního stavu na konkrétní podmínky.
Poznámka: Výjimka IllegalStateException se neomezuje pouze na framework Collections.
Podívejme se na některé ze scénářů, kde IllegalStateException bude vrženo.
Příklad 1:
Následující Java program znázorňuje situaci, kdy se pokoušíme volat metodu start(), když je metoda run() již spuštěna.
IllegalStateExceptionTest1.java
parseint java
// importing necessary packages import java.io.*; import java.util.*; // creating a new thread in NewThread class by extending Thread class // below class is acts as a helper class class NewThread extends Thread { // declaring the run() method // executes 3 times using for loop public void run() { for (int i = 0; i <3; i++) { displaying the text system.out.println('this is example of illegalstateexception'); } creating main class public illegalstateexceptiontest1 method static void main(string[] args) object above helper newthread t="new" newthread(); starting created thread using start() t.start(); thread'); again when it already running this gives an exception < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/19/how-resolve-illegalstateexception-java.webp" alt="How to resolve IllegalStateException in Java"> <h3>Example 2:</h3> <p>The following code depicts the situation where we call the start() method on a thread when the execution of run() method is over.</p> <p> <strong>IllegalStateExceptionTest2.java</strong> </p> <pre> // importing necessary packages import java.io.*; import java.util.*; // creating a new thread in NewThread class by extending Thread class // below class is acts as a helper class class NewThread extends Thread { // declaring the run() method // executes 3 times using for loop public void run() { for (int i = 0; i <3; i++) { displaying the text system.out.println('this is example of illegalstateexception'); } creating main class public illegalstateexceptiontest2 method static void main(string[] args) object above helper newthread t="new" newthread(); starting created thread using start() t.start(); try system.out.println('main going to sleep'); putting on sleep for 4000ms t.sleep(4000); awaken'); catch (exception e) system.out.println(e); message thread'); calling over a dead this also gives an exception < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/19/how-resolve-illegalstateexception-java-2.webp" alt="How to resolve IllegalStateException in Java"> <h3>Example 3:</h3> <p>The following code explains the situation where we are using the remove() method to remove the element from the ArrayList, before moving to the first element.</p> <p> <strong>IllegalStateExceptionTest3.java</strong> </p> <pre> // importing necessary packages import java.util.ArrayList; import java.util.ListIterator; // creating the main class public class IllegalStateExceptionTest3 { // main method public static void main(String args[]) { // instantiating the object of ArrayList ArrayList list = new ArrayList(); // adding elements to the ArrayList list.add('Nirnay'); list.add('Anu'); list.add('Swara'); list.add('Pavan'); // creating the iterator object to iterate the list ListIterator it = list.listIterator(); // removing the element without moving to first position // gives an exception it.remove(); } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/19/how-resolve-illegalstateexception-java-3.webp" alt="How to resolve IllegalStateException in Java"> <h2>Solution for the IllegalStateException</h2> <p>To avoid the <strong>java.lang.IllegalStateException</strong> in Java we should take care that any method in our code cannot be called at inappropriate or illegal time.</p> <p> <strong>Solution for example 1 and 2:</strong> </p> <p>Consider the above example 1 and 2 where we have called the start() method more than once. If we call it only once, we will not get this exception. Because start() method is not called after starting the thread.</p> <p> <strong>IllegalStateExceptionSolution.java</strong> </p> <pre> // importing necessary packages import java.io.*; import java.util.*; // creating a new thread in NewThread class by extending Thread class // below class is acts as a helper class class NewThread extends Thread { // declaring the run() method // executes 3 times using for loop public void run() { for (int i = 0; i <3; i++) { displaying the text system.out.println('this is example of illegalstateexception'); } creating main class public illegalstateexceptionsolution method static void main(string[] args) object above helper newthread t="new" newthread(); starting created thread using start() t.start(); try system.out.println('main going to sleep'); putting on sleep for 4000ms t.sleep(4000); awaken'); catch (exception e) system.out.println(e); message thread'); we do not call again < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/19/how-resolve-illegalstateexception-java-4.webp" alt="How to resolve IllegalStateException in Java"> <p> <strong>Solution for Example 3:</strong> </p> <p>The remove() method of the ArrayList class is used to remove the last element after calling the next() method.</p> <ul> <li>After removing element at current index we have to move next element to remove it. (for every call of the next() method, we have to invoke the remove() method only once).</li> <li>As the initial position of list will be before the first element, we cannot call the remove() method without calling the next() method.</li> </ul> <p>In order to prevent the exception we need to follow the above steps in our Java code.</p> <p> <strong>IllegalStateExceptionSolution2.java</strong> </p> <pre> // importing necessary packages import java.util.ArrayList; import java.util.ListIterator; // creating the main class public class IllegalStateExceptionSolution2 { // main method public static void main(String args[]) { // instantiating the object of ArrayList ArrayList list = new ArrayList(); // adding elements to the ArrayList list.add('Nirnay'); list.add('Anu'); list.add('Swara'); list.add('Pavan'); // creating the iterator object to iterate the list ListIterator it = list.listIterator(); // using the next() method before remove() it.next(); // removing the element without moving to first position it.remove(); // displaying new ArrayList System.out.println('List after removing the first element: ' + list); } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/19/how-resolve-illegalstateexception-java-5.webp" alt="How to resolve IllegalStateException in Java"> <hr></3;></pre></3;></pre></3;>
Výstup:
Řešení pro výjimku IllegalStateException
Abyste se vyhnuli java.lang.IllegalStateException v Javě bychom měli dbát na to, aby žádná metoda v našem kódu nemohla být volána v nevhodný nebo nezákonný čas.
Řešení například 1 a 2:
Uvažujme výše uvedený příklad 1 a 2, kde jsme metodu start() volali více než jednou. Pokud jej zavoláme pouze jednou, tuto výjimku nedostaneme. Protože metoda start() není volána po spuštění vlákna.
linuxové klávesové zkratky
IllegalStateExceptionSolution.java
// importing necessary packages import java.io.*; import java.util.*; // creating a new thread in NewThread class by extending Thread class // below class is acts as a helper class class NewThread extends Thread { // declaring the run() method // executes 3 times using for loop public void run() { for (int i = 0; i <3; i++) { displaying the text system.out.println(\'this is example of illegalstateexception\'); } creating main class public illegalstateexceptionsolution method static void main(string[] args) object above helper newthread t="new" newthread(); starting created thread using start() t.start(); try system.out.println(\'main going to sleep\'); putting on sleep for 4000ms t.sleep(4000); awaken\'); catch (exception e) system.out.println(e); message thread\'); we do not call again < pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/19/how-resolve-illegalstateexception-java-4.webp" alt="How to resolve IllegalStateException in Java"> <p> <strong>Solution for Example 3:</strong> </p> <p>The remove() method of the ArrayList class is used to remove the last element after calling the next() method.</p> <ul> <li>After removing element at current index we have to move next element to remove it. (for every call of the next() method, we have to invoke the remove() method only once).</li> <li>As the initial position of list will be before the first element, we cannot call the remove() method without calling the next() method.</li> </ul> <p>In order to prevent the exception we need to follow the above steps in our Java code.</p> <p> <strong>IllegalStateExceptionSolution2.java</strong> </p> <pre> // importing necessary packages import java.util.ArrayList; import java.util.ListIterator; // creating the main class public class IllegalStateExceptionSolution2 { // main method public static void main(String args[]) { // instantiating the object of ArrayList ArrayList list = new ArrayList(); // adding elements to the ArrayList list.add('Nirnay'); list.add('Anu'); list.add('Swara'); list.add('Pavan'); // creating the iterator object to iterate the list ListIterator it = list.listIterator(); // using the next() method before remove() it.next(); // removing the element without moving to first position it.remove(); // displaying new ArrayList System.out.println('List after removing the first element: ' + list); } } </pre> <p> <strong>Output:</strong> </p> <img src="//techcodeview.com/img/java-tutorial/19/how-resolve-illegalstateexception-java-5.webp" alt="How to resolve IllegalStateException in Java"> <hr></3;>
Výstup:
3;>3;>3;>