logo

Hoe IllegalStateException in Java op te lossen?

Een uitzondering is een ongewenste en onverwachte fout die in het programma wordt gegenereerd. Meestal treedt er een uitzondering op wanneer er een fout in onze code zit, maar deze kan worden afgehandeld. Het verstoort de normale stroom van de code.

De code genereert bijvoorbeeld een uitzondering als de gebruiker ongeldige informatie heeft ingevoerd, als de code het bestand op de externe locatie niet kan lezen of als de netwerkverbinding midden in de communicatie wordt verbroken.

beste hentai

IllegalStateException in Java

IllegaleStateException is de subklasse van de klasse RuntimeException en is daarom een ​​niet-gecontroleerde uitzondering. Het wordt expliciet aan de orde gesteld door de programmeur of door de API-ontwikkelaar. Het wordt gegenereerd wanneer een methode-aanroep illegaal is of een methode op een verkeerd tijdstip wordt aangeroepen.

Zodra we bijvoorbeeld een thread starten, kunnen we dezelfde thread niet opnieuw starten; als we dat proberen te doen, genereert het een runtime-uitzondering, d.w.z. IllegaleStateException .

De uitzondering kunnen komen meestal in de code voor als we met het Collections-framework werken. De Lijst, Wachtrij, Kaarten en Boom zijn enkele van de collecties. Hiervan hebben Lijst en Wachtrijen de neiging om de uitzondering op de illegale staat toe te passen op de specifieke omstandigheden.

Opmerking: de IllegalStateException-uitzondering is niet alleen beperkt tot het Collections-framework.

Laten we eens kijken naar een scenario waarin de IllegaleStateException zal worden gegooid.

Voorbeeld 1:

Het volgende Java-programma toont de situatie waarin we de methode start() proberen aan te roepen terwijl de methode run() al wordt uitgevoerd.

IllegalStateExceptionTest1.java

onderstrepen met css
 // 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(&apos;Nirnay&apos;); list.add(&apos;Anu&apos;); list.add(&apos;Swara&apos;); list.add(&apos;Pavan&apos;); // 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(&apos;Nirnay&apos;); list.add(&apos;Anu&apos;); list.add(&apos;Swara&apos;); list.add(&apos;Pavan&apos;); // 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(&apos;List after removing the first element: &apos; + 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;>

Uitgang:

Java-wiskunde
Hoe IllegalStateException in Java op te lossen

Oplossing voor de IllegalStateException

Om te voorkomen dat java.lang.IllegalStateException in Java moeten we ervoor zorgen dat elke methode in onze code niet op een ongepast of illegaal tijdstip kan worden aangeroepen.

Oplossing voor voorbeeld 1 en 2:

Beschouw het bovenstaande voorbeeld 1 en 2 waarin we de methode start() meer dan eens hebben aangeroepen. Als we het maar één keer aanroepen, krijgen we deze uitzondering niet. Omdat de methode start() niet wordt aangeroepen na het starten van de thread.

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(&apos;Nirnay&apos;); list.add(&apos;Anu&apos;); list.add(&apos;Swara&apos;); list.add(&apos;Pavan&apos;); // 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(&apos;List after removing the first element: &apos; + 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;>

Uitgang:

Hoe IllegalStateException in Java op te lossen