Is It Illegal Not To Register To Vote
An unexpected, unwanted event that disturbed the normal flow of a program is called Exception .
Nearly of the time exception is caused by our plan and these are recoverable. Instance: If our plan requirement is to read data from the remote file locating in U.S.A. At runtime, if a remote file is non available then we will get RuntimeException saying fileNotFoundException. If fileNotFoundException occurs nosotros can provide the local file to the program to read and proceed the residual of the program normally.
There are mainly two types of exception in java as follows:
1. Checked Exception:
The exception which is checked by the compiler for the smooth execution of the programme at runtime is chosen a checked exception. In our programme, if there is a take chances of rising checked exception then compulsory we should handle that checked exception (either past try-take hold of or throws keyword) otherwise nosotros will get compile-time fault.
Examples of checked exceptions are ClassNotFoundException, IOException, SQLException, etc.
2. Unchecked Exception:
The exceptions which are not checked by the compiler, whether programmer handling or not such type of exception are called an unchecked exception.
Examples of unchecked Exceptions are ArithmeticException, ArrayStoreException etc.
Whether the exception is checked or unchecked every exception occurs at run fourth dimension simply if in that location is no chance of occurring any exception at compile fourth dimension.
IllegalStateException is the child form of RuntimeException and hence it is an unchecked exception. This exception is rising explicitly by developer or by the API developer to indicate that a method has been invoked at the wrong time. Mostly, this method is used to indicate a method is called at an illegal or inappropriate fourth dimension.
Example: Subsequently starting a thread we are not immune to restart the same thread over again otherwise we volition become Runtime Exception maxim IllegalStateException.
Example 1: We call start() method when information technology'due south already executing the run() method.
Java
import
java.io.*;
import
java.util.*;
class
myThread
extends
Thread {
public
void
run()
{
for
(
int
i =
0
; i <
5
; i++) {
System.out.println(
"GeeksForGeeks"
);
}
}
}
class
Thread1 {
public
static
void
principal(String[] args)
{
myThread t =
new
myThread();
t.start();
System.out.println(
"Chief Thread"
);
t.start();
}
}
Example 2: We call starting time() method on a thread when it has finished executing run() method.
Java
import
coffee.io.*;
import
java.util.*;
course
myThread
extends
Thread {
public
void
run()
{
for
(
int
i =
0
; i <
v
; i++) {
System.out.println(
"GeeksForGeeks"
);
}
}
}
class
Thread1 {
public
static
void
main(Cord[] args)
{
myThread t =
new
myThread();
t.kickoff();
effort
{
System.out.println(
"Main Thread is going to sleep"
);
t.sleep(
2000
);
Organisation.out.println(
"Master Thread awaken"
);
}
catch
(Exception e) {
System.out.println(e);
}
System.out.println(
"Main Thread"
);
t.offset();
}
}
How to solve this mistake?
In guild to avoid java.lang.IllegalStateException in Java master Thread nosotros must ensure that any method in our lawmaking cannot exist called at an illegal or an inappropriate time.
In the above example if we phone call commencement() method only one time on thread t so nosotros will not go any coffee.lang.IllegalStateException considering we are not calling the start() method later the starting of thread (i.e we are not calling the start() method at an illegal or an inappropriate fourth dimension.)
Java
import
java.io.*;
import
java.util.*;
class
myThread
extends
Thread {
public
void
run()
{
for
(
int
i =
0
; i <
v
; i++) {
Arrangement.out.println(
"GeeksForGeeks"
);
}
}
}
class
Thread1 {
public
static
void
main(String[] args)
{
myThread t =
new
myThread();
t.start();
endeavour
{
Organisation.out.println(
"Main Thread is going to slumber"
);
t.sleep(
2000
);
Arrangement.out.println(
"Master Thread awaken"
);
}
catch
(Exception eastward) {
System.out.println(eastward);
}
System.out.println(
"Main Thread"
);
}
}
Source: https://www.geeksforgeeks.org/how-to-solve-java-lang-illegalstateexception-in-java-main-thread/
Posted by: kelleyinack1988.blogspot.com
0 Response to "Is It Illegal Not To Register To Vote"
Post a Comment