0% found this document useful (0 votes)
276 views3 pages

Java: Throw, Throws & Nested Try Explained

This document contains a summary of key concepts related to exceptions in Java including throw, throws, and nested try blocks. It provides 8 multiple choice questions about using throw to explicitly generate exceptions, the Throwable class, using new to create exception instances, using throws in method declarations, and how finally blocks are always executed regardless of exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
276 views3 pages

Java: Throw, Throws & Nested Try Explained

This document contains a summary of key concepts related to exceptions in Java including throw, throws, and nested try blocks. It provides 8 multiple choice questions about using throw to explicitly generate exceptions, the Throwable class, using new to create exception instances, using throws in method declarations, and how finally blocks are always executed regardless of exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Java Questions & Answers � Throw, Throws & Nested Try

This section of our 1000+ Java MCQs focuses on throw, throws & nested try of Java
Programming Language.

1. Which of these keywords is used to generate an exception explicitly?


a) try
b) finally
c) throw
d) catch
View Answer

Answer: c
Explanation: None.
2. Which of these class is related to all the exceptions that are explicitly
thrown?
a) Error
b) Exception
c) Throwable
d) Throw
View Answer

Answer: c
Explanation: None.
3. Which of these operator is used to generate an instance of an exception than can
be thrown by using throw?
a) new
b) malloc
c) alloc
d) thrown
View Answer

Answer: a
Explanation: new is used to create an instance of an exception. All of java�s built
in run-time exceptions have two constructors: one with no parameters and one that
takes a string parameter.
4. Which of these keywords is used to by the calling function to guard against the
exception that is thrown by called function?
a) try
b) throw
c) throws
d) catch
View Answer

Answer: c
Explanation: If a method is capable of causing an exception that it does not
handle. It must specify this behaviour the behaviour so that callers of the method
can guard themselves against that exception. This is done by using throws clause in
methods declaration.
5. What will be the output of the following Java code?

class exception_handling
{
public static void main(String args[])
{
try
{
int a = [Link];
int b = 10 / a;
[Link](a);
try
{
if (a == 1)
a = a / a - a;
if (a == 2)
{
int []c = {1};
c[8] = 9;
}
}
catch (ArrayIndexOutOfBoundException e)
{
[Link]("TypeA");
}
catch (ArithmeticException e)
{
[Link]("TypeB");
}
}
}
}
a) TypeA
b) TypeB
c) Compile Time Error
d) 0TypeB
View Answer

Answer: c
Explanation: Because we can�t go beyond array limit
6. What will be the output of the following Java code?

class exception_handling
{
public static void main(String args[])
{
try
{
[Link]("A");
throw new NullPointerException ("Hello");
}
catch(ArithmeticException e)
{
[Link]("B");
}
}
}
a) A
b) B
c) Hello
d) Runtime Exception
View Answer

Answer: d
Explanation: None.
Output:
$ javac exception_handling.java
$ java exception_handling
Exception in thread "main" [Link]: Hello
at exception_handling.main
7. What will be the output of the following Java code?

advertisement
public class San
{
public static void main(String[] args)
{
try
{
return;
}
finally
{
[Link]( "Finally" );
}
}
}
a) Finally
b) Compilation fails
c) The code runs with no output
d) An exception is thrown at runtime
View Answer

Answer: a
Explanation: Because finally will execute always.
8. What will be the output of the following Java code?

public class San


{
public static void main(String args[])
{
try
{
[Link]("Hello world ");
}
finally
{
[Link]("Finally executing ");
}
}
}
a) The program will not compile because no exceptions are specified
b) The program will not compile because no catch clauses are specified
c) Hello world
d) Hello world Finally executing
View Answer

Answer: d
Explanation: None

Common questions

Powered by AI

In Java, a program can compile and run successfully without a 'catch' block if a 'try' block is followed by a 'finally' block. Since 'finally' executes after the 'try' block, it ensures proper execution of important code like resource release, regardless of whether an exception is thrown or caught. Such a scenario might occur if the developer only requires cleanup actions, even if exception handling itself isn't specified explicitly within 'catch' blocks. This is permissible for unchecked exceptions like RuntimeExceptions .

In Java, if a 'try' block is followed by a 'catch' block, any exceptions thrown within are caught by the 'catch' block, allowing for specific exception handling. However, if it is followed by a 'finally' block, the code within 'finally' will execute after the 'try' block regardless of whether an exception was thrown or handled. This ensures resource release or other cleanup actions. Thus, using both 'catch' and 'finally' allows for exception management and guaranteed execution of cleanup code .

The 'finally' block in Java is used to execute important code such as releasing resources regardless of whether an exception is thrown or not. It differs from the 'catch' block, which is designed to handle the exception if it occurs. The 'finally' block is executed after the try and catch blocks have executed, and it will execute even if an exception is not caught or if a 'return' statement is encountered in the try block .

The 'Error' class in Java is used for serious problems that a reasonable application should not try to catch, such as internal JVM errors. On the other hand, the 'Exception' class is used to handle conditions that a program might want to catch, such as input/output errors. While 'Errors' generally signify issues that are beyond the application's control and are not meant to be caught by ordinary applications, 'Exceptions' are meant to be managed and controlled by the application's code to maintain smooth execution flow .

The keyword used to explicitly generate an exception in Java is 'throw'. It is used within a block of code to create an exception object and pass it to the runtime system. This is often done to indicate an erroneous or exceptional situation that needs to be handled. By explicitly throwing an exception, developers can enforce business rules or catch violations at runtime .

In Java, unchecked exceptions (subclasses of RuntimeException) are not required to be declared using the 'throws' clause in method definitions because they can occur during runtime regardless of method calls being correct or not. While 'try' blocks can be used to catch and handle these exceptions during execution, 'throws' is used for checked exceptions to indicate that a method could potentially throw an exception and the caller needs to handle it. This distinction is crucial because unchecked exceptions usually indicate programming errors, whereas checked exceptions represent recoverable conditions .

The 'new' keyword is used to create an instance of an exception class, which can then be explicitly thrown using the 'throw' statement in Java. This facilitates exception handling by allowing developers to create and throw custom exceptions at specific points in their code to indicate exceptional conditions or business rule violations. The coupling of 'new' with 'throw' offers precise control over exception instantiation and propagation .

Even when a 'return' statement is executed inside a 'try' block, Java ensures that the 'finally' block is executed before the method returns control. This behavior guarantees that all cleanup code within the 'finally' block is executed, maintaining resource integrity and preventing resource leaks, which is a crucial aspect of robust programming practices .

Using the 'throws' keyword in a method declaration in Java signifies that the method does not handle a particular exception and passes the responsibility onto the calling method. This influences method design by mandating the caller to provide exception handling, which adds a layer of robustness and accountability. It encourages developers to anticipate and design for exceptional circumstances, thus improving error propagation modeling and helping in maintaining modular and maintainable code. Strategically, it allows developers to choose where to catch and handle exceptions, potentially centralizing exception handling logic .

A nested 'try' block is beneficial in Java programming because it allows for more granular control over the handling of exceptions that may occur in different parts of a single method. This is necessary when different blocks of code have different exception handling requirements or when subsequent code execution depends on successful completion of the inner block. It also aids in separating concerns and improving code readability by localizing specific exception handling logic closer to the place where exceptions might arise .

You might also like