0% found this document useful (0 votes)
8 views5 pages

Chapter 4

The document discusses errors and exceptions in Java programming. It defines error and exception, lists common exception types, and explains exception handling using try, catch, and finally blocks. It also covers thread states and the two ways to create threads in Java.

Uploaded by

Yash Gaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Chapter 4

The document discusses errors and exceptions in Java programming. It defines error and exception, lists common exception types, and explains exception handling using try, catch, and finally blocks. It also covers thread states and the two ways to create threads in Java.

Uploaded by

Yash Gaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter 4

1.Define error. List types of error.


Ans.
1. Errors are mistakes that can make a program go wrong.
2. Errors may be logical or may be typing mistakes.
3. An error may produce an incorrect output or may terminate the execution of the
program abruptly or even may cause the system to crash.
4. Errors are broadly classified into two categories:
1. Compile time errors
2. Runtime errors
2.Define exception. State built-in exceptions.
Ans.
1. An exception is a problem that arises during the execution of a program.
2. Java exception handling is used to handle error conditions in a program
systematically by taking the necessary action
Built-in exceptions:
1.Arithmetic exception:
Arithmetic error such as division by zero.
2.ArrayIndexOutOfBounds Exception:
Array index is out of bound
3.ClassNotFoundException
4.FileNotFoundException:
Caused by an attempt to access a non-existent file.
5.IO Exception:
Caused by general I/O failures, such as inability to read from a file.
6.NullPointerException:
Caused by referencing a null object.
7.NumberFormatException:
Caused when a conversion between strings and number fails.
8.StringIndexOutOfBoundsException:
Caused when a program attempts to access a non-existent character position in a string.
9.OutOfMemoryException:
Caused when there’s not enough memory to allocate a new object.
10.SecurityException:
Caused when an applet tries to perform an action not allowed by the browser’s security
setting.
11.StackOverflowException:
Caused when the system runs out of stack space.
3.How Exception is handled in java.
Ans.
try:
This block applies a monitor on the statements written inside it. If there exist any exception,
the control is transferred to catch or finally block.
catch:
This block includes the actions to be taken if a particular exception occurs
finally:
finally block includes the statements which are to be executed in any case, in case the
exception is raised or not.
throw:
This keyword is generally used in case of user defined exception, to forcefully raise the
exception and take the required action.
throws:
throws keyword can be used along with the method definition to name the list of exceptions
which are likely to happen during the execution of that method. In that case, try … catch
block is not necessary in the code

4.Write the syntax of try-catch-finally blocks.


Ans.
Try
{
//Statements to be monitored for any exception
}
Catch (ThrowableInstance1 obj)
{
//Statements to execute if this type of exception occurs
}
Catch (ThrowableInstance2 obj2)
{
//Statements
}
Finally
{
//Statements which should be executed even if any exception happens
}

5.Explain the two ways of creating threads in Java.


Ans.
Thread is a independent path of execution within a program.
There are two ways to create a thread:
1. By extending the Thread class:
1. Thread class provide constructors and methods to create and perform operations on
a thread.
2. This class implements the Runnable interface.
3. When we extend the class Thread, we need to implement the method run().
4. Once we create an object, we can call the start() of the thread class for executing the
method run().
2.By implementing the runnable interface:
Runnable interface has only on one method- run().

6.Explain life cycle of thread.


Ans.
Thread Life Cycle :
Thread has five different states throughout its life.
1. Newborn State
2. Runnable State
3. Running State
4. Blocked State
5. Dead State
Thread should be in any one state of above and it can be move from one state to another by
different methods and ways.
Newborn state:
1. When a thread object is created it is said to be in a new born state.
2. When the thread is in a new born state it is not scheduled running from this state it
can be scheduled for running by start() or killed by stop().
3. If put in a queue it moves to runnable state.
Runnable State:
1. It means that thread is ready for execution and is waiting for the availability of the
processor i.e. the thread has joined the queue and is waiting for execution.
2. If all threads have equal priority, then they are given time slots for execution in round
robin fashion.
3. The thread that relinquishes control joins the queue at the end and again waits for its
turn.
4. A thread can relinquish the control to another before its turn comes by yield().
Running State:
1. It means that the processor has given its time to the thread for execution.
2. The thread runs until it relinquishes control on its own or it is pre-empted by a higher
priority thread.
Blocked state:
1. A thread can be temporarily suspended or blocked from entering into the runnable
and running state by using either of the following thread method.
1) suspend() :
Thread can be suspended by this method. It can be rescheduled by resume().

2) wait():
If a thread requires to wait until some event occurs, it can be done using wait method
and can be scheduled to run again by notify().

3) sleep():
We can put a thread to sleep for a specified time period using sleep(time) where time
is in ms.
It re-enters the runnable state as soon as period has elapsed /over
Dead State:
1. Whenever we want to stop a thread form running further we can call its stop().
2. The statement causes the thread to move to a dead state.
3. A thread will also move to dead state automatically when it reaches to end of the
method.
4. The stop method may be used when the premature death is required.

You might also like