DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
BE- Computer Science and Engineering
Anna University Regulation: 2021
CS3391- Object Oriented Programming
II Year/III Semester
Question Bank
Unit-III Exception handling and Multithreading
Prepared By,
Mrs.J.Sweetline Arputham, AP/CSE
4931_GRACE College of Engineering,Thoothukudi
UNIT III
Exception Handling and Multithreading
PART A
1. Write down the purpose of exception handling mechanism.
The main purpose of exception handling mechanism is used to detect and report an
“exceptional circumstance” so that necessary action can be taken. It performs the following
tasks 5. Find the problem(Hit the exception) 6. Inform that an error occurred(throw the
exception). 7. Receive the error information(Catch the exception) 8. Take corrective
actions(Handle the exception)
2. What are the types of exceptions?
There are two types of exceptions 1. Predefined Exceptions-The Exceptions which are
predefined are called predefined exceptions 2. Userdefined Exceptions- The Exceptions which
are defined by the user are called userdefined exceptions
3. How the exception handling is managed?
Java exception handling is managed via five keywords. • try • catch • throw • throws and •
finally
4. Write down the general form of an exception-handling block.
The general form of an exception-handling block
try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb)
{
// exception handler for ExceptionType2
}
//
...
finally
{
// block of code to be executed after try block ends
}
5. Write down the use of try and catch block in exception handling.
The try block allows us to fix the errors. Catch block prevents the program from
automatically terminating. To handle a run-time error, enclose the code to be monitored inside
a try block. After the try block, include a catch block that specifies the exception type that
needs to be caught.
Syntax:
try
{
statement;
CS3391_OOP
4931_GRACE College of Engineering,Thoothukudi
}
catch(Exception-type exOb)
{
statement;
}
6. Explain the situation where we need to use multiple catch clauses.
In some situation, more than one exception can occur by a single piece of code. To handle this
situation, we can use two or more catch clauses, each catching a different type of exception.
When an exception is thrown, each catch block is executed in order, and the first one whose
type matches that exception is executed.After one catch block executes, the others are
bypassed, and continues after the try/catch block.
7. Write down the syntax for multiple catch clauses.
The syntax for multiple catch clauses
try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb)
{
// exception handler for ExceptionType2
}
8. Explain the situation where we need to use nested try statements.
The try statement can be nested. That is, a try statement can be inside the block of another try.
Each time a try statement is entered, the context of that exception is pushed on the stack. If an
inner try statement does not have a catch handler for a particular exception, the stack is
unwound and
the next try statement’s catch handlers are inspected for a match. This continues until one of
the catch statements succeeds, or until all of the nested try statements are exhausted. If no
catch statement matches, then the Java run-time system will handle the exception.
9. Write down the syntax for nested try statement.
The syntax for nested try statement:
//Main try block
try
{
statement 1;
statement 2;
//try-catch block inside another try block
try
{
statement 3;
statement 4;
//try-catch block inside nested try block
CS3391_OOP
4931_GRACE College of Engineering,Thoothukudi
try
{
statement 5;
statement 6;
}
catch(Exception e2)
{
//Exception Message
}
}
catch(Exception e1)
{
//Exception Message
}
}
//Catch of Main(parent) try block
catch(Exception e3)
{
//Exception Message
}
10. Write down the use of throw statement.
The throw keyword in Java is used to explicitly throw an exception from a method or any
block of code. We can throw either checked or unchecked exception.
Syntax:
throw new exception_class("error message");
For example:
throw new ArithmeticException("dividing a number by 5 is not allowed in this program");
11. Write down the use of throws clause.
Using throws clause, We can list the types of exceptions that a method might throw.The
exceptions which are thrown in a method might be using throws clause. If they are not, a
compile-time error will result.
Syntax:
type method-name(parameter-list) throws exception-list
{
// body of method
}
Exception-list is a comma-separated number of exceptions that a method can throw.
12. Write down the use of finally clause.
finally creates a block of code that is to be executed after a try/catch block has completed its
execution.The finally block will execute if an exception is thrown or not thrown. The finally
clause is optional.Each try block requires either one catch or a finally clause
Syntax:
try
{
//Statements that may cause an exception
}
catch
CS3391_OOP
4931_GRACE College of Engineering,Thoothukudi
{
//Handling exception
}
finally
{
//Statements to be executed
}
13. What is Unchecked Exception?
These are the exceptions that are not checked at compiled time.
14. What is Checked Exception?
These are the exceptions that are checked at compile time. If some code within a method
throws a checked exception, then the method must either handle the exception or it must
specify the exception using throws keyword.
15. Explain the way to create own exceptions.
We can throw our own exceptions using throw keyword.
Syntax:
throw new Throwable_subclass;
Eg:
throw new ArithmeticException;
16. What is the purpose of the start method in Java threads?
The start method is used to begin the execution of a thread. When the start method is invoked
on a Thread object, it causes the run method of the thread to be executed in a separate thread
of control.
17. Explain the significance of thread priorities in Java.
Thread priorities in Java represent the preference of a thread to execute before or after another
thread. Priority values range from 1 to 10, where 1 is the lowest priority and 10 is the highest.
The Java Virtual Machine (JVM) uses these priorities to schedule the execution of threads.
Higher-priority threads are given preference, but it's important to note that thread priorities are
only hints to the scheduler, and the actual behavior might vary across different platforms.
18. How is synchronization achieved in Java multithreading, and why is it important?
Synchronization in Java is achieved using the synchronized keyword. When a method or a
block of code is declared as synchronized, only one thread can execute it at a time. This
prevents multiple threads from interfering with each other and ensures that shared resources
are accessed safely. Synchronization is crucial to avoid race conditions and to maintain data
consistency in a multithreaded environment.
19. What is the purpose of the wait, notify, and notifyAll methods in Java multithreading?
These methods are used for inter-thread communication in Java.
wait: Causes the current thread to release the lock and wait until another thread notifies it.
notify: Wakes up one of the threads that are currently waiting for the object's monitor.
notifyAll: Wakes up all the threads that are currently waiting for the object's monitor. It is
generally preferred to use notifyAll to avoid potential deadlocks.
20. Explain the concept of auto boxing in Java.
Auto boxing is the automatic conversion of primitive data types to their corresponding
wrapper classes, and vice versa, by the Java compiler. For example, converting an int to an
Integer or a double to a Double. Auto boxing simplifies code and makes it more readable by
allowing developers to use primitive types and their wrapper classes interchangeably without
explicitly calling methods for conversion.
CS3391_OOP
4931_GRACE College of Engineering,Thoothukudi
PART B
1. Explain the Throwing and Catching Exception.
2. What is exception? How to throw an exception? Give an example.
3. What is finally class? How to catch exceptions? Write an example.
4. What is meant by exceptions? Why it is needed? Describe the exception hierarchy.
5. Write note on Stack Trace Elements. Give example.
6. Define Exception and explain its different types with example.
7. Discuss about character stream classes.
8. What is multithreading? Explain with an example program
9. What are the different states of a thread? Explain
10. What are the ways of creating a thread? Explain with program
11. Explain synchronization of thread with example program
12. List the five keywords in Java exception handling. Describe the use of the keywords.
CS3391_OOP