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

Java Exception Types Overview

This document discusses exception types in Java, including: 1) The Throwable class is the super class of all exception classes. 2) The Exception class is related to exceptions that can be caught using try/catch. 3) The Error class is related to runtime errors that usually cannot be caught. 4) When no catch block is used, the Java runtime system handles the exception using a default exception handler.
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)
36 views3 pages

Java Exception Types Overview

This document discusses exception types in Java, including: 1) The Throwable class is the super class of all exception classes. 2) The Exception class is related to exceptions that can be caught using try/catch. 3) The Error class is related to runtime errors that usually cannot be caught. 4) When no catch block is used, the Java runtime system handles the exception using a default exception handler.
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 � Exceptions Types

This section of our 1000+ Java MCQs focuses on Exceptions types in Java Programming
Language.

1. Which of these is a super class of all exceptional type classes?


a) String
b) RuntimeExceptions
c) Throwable
d) Cacheable
View Answer

Answer: c
Explanation: All the exception types are subclasses of the built in class
Throwable.
2. Which of these class is related to all the exceptions that can be caught by
using catch?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
View Answer

Answer: b
Explanation: Error class is related to java run time error that can�t be caught
usually, RuntimeExecption is subclass of Exception class which contains all the
exceptions that can be caught.
3. Which of these class is related to all the exceptions that cannot be caught?
a) Error
b) Exception
c) RuntimeExecption
d) All of the mentioned
View Answer

Answer: a
Explanation: Error class is related to java run time error that can�t be caught
usually, RuntimeExecption is subclass of Exception class which contains all the
exceptions that can be caught.
4. Which of these handles the exception when no catch is used?
a) Default handler
b) finally
c) throw handler
d) Java run time system
View Answer

Answer: a
Explanation: None.
5. What exception thrown by parseInt() method?
a) ArithmeticException
b) ClassNotFoundException
c) NullPointerException
d) NumberFormatException
View Answer

Answer: d
Explanation: parseInt() method parses input into integer. The exception thrown by
this method is NumberFormatException.
6. What will be the output of the following Java code?

class exception_handling
{
public static void main(String args[])
{
try
{
[Link]("Hello" + " " + 1 / 0);
}
finally
{
[Link]("World");
}
}
}
a) Hello
b) World
c) Compilation Error
d) First Exception then World
View Answer

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

advertisement

class exception_handling
{
public static void main(String args[])
{
try
{
int i, sum;
sum = 10;
for (i = -1; i < 3 ;++i)
{
sum = (sum / i);
[Link](i);
}
}
catch(ArithmeticException e)
{
[Link]("0");
}
}
}
a) -1
b) 0
c) -10
d) -101
View Answer

Answer: c
Explanation: For the 1st iteration -1 is displayed. The 2nd exception is caught in
catch block and 0 is displayed.
Output:
$ javac exception_handling.java
$ java exception_handling
-10

Common questions

Powered by AI

The Throwable class in Java is the superclass of all errors and exceptions. It acts as the root of the exception class hierarchy, meaning every exception type is a subclass of Throwable .

In Java, the Error class is associated with exceptions related to system and application errors that are typically beyond the programmer's control and cannot be caught or managed using catch clauses. As such, they indicate serious problems that a reasonable application should not try to catch. This has significant implications for exception handling as it requires developers to recognize Errors as critical issues that typically point to significant failures in the execution environment .

Checked exceptions in Java require catching or declaring in the method signature, indicating that the method directly deals with these error conditions. These exceptions occur at compile-time and are foreseeable. On the other hand, unchecked exceptions, which include RuntimeException and its subclasses, occur at runtime and do not require mandatory handling. The distinction is important as it affects how developers write and maintain code, ensuring known risks are managed while allowing flexibility in handling unforeseen issues .

When an exception is not explicitly caught in Java, the default exception handler, provided by the Java runtime system, takes over the handling of the exception. It prints the stack trace of the exception on the console and terminates the program. This default mechanism ensures that unhandled exceptions do not pass quietly and unknown errors can be traced .

RuntimeException is a subclass of Exception but not typically caught since these exceptions are usually the result of programming errors, such as logic flaws or improper use of an API. While Error signals problems arising from the system that are outside a programmer's direct control, Exception class encompasses checked exceptions that should be caught or declared in the method signature. This distinction highlights RuntimeException's role as representing errors that can be avoided through correct programming practices .

The class hierarchy for exceptions in Java allows for organized and efficient error handling. By categorizing exceptions into a hierarchy with Throwable at the root, it facilitates polymorphism in catching exceptions, promotes reusable and modular code, and supports clear semantics for different error types (checked vs unchecked). This hierarchy helps developers anticipate potential exceptions, customize exception handling to specific needs, and ensure reliable and maintainable code execution .

In Java, when an exception is thrown inside a try block and there is no catch clause, the finally block is executed regardless. For example, if a divide-by-zero exception occurs in a try block, as demonstrated in the sample code provided, the system will still execute the finally block after printing the exception stack trace. This ensures cleanup actions defined in the finally block are always carried out, even if an exception occurs .

A NumberFormatException in Java occurs when there is an attempt to convert a string to a numeric type but the string does not have an appropriate format. This exception needs to be caught or declared when using methods like parseInt() that convert strings to numbers. The implications include ensuring that inputs are validated before parsing to prevent application failure due to invalid data, maintaining robustness and user-friendly software .

ArithmeticException, such as a division by zero error, disrupts normal control flow by terminating the execution of the current try block and transferring control to the corresponding catch block if present. If no catch block is specified, the default exception handler will terminate the program with an error message. In provided examples, the occurrence of ArithmeticException demonstrates how the exception handling mechanisms redirect control flow to maintain program stability and provide error reporting .

Try-catch-finally constructs in Java enhance application reliability by encapsulating error-prone operations within try blocks, capturing exceptions via catch blocks for controlled handling, and executing cleanup code in finally blocks. This structure helps maintain application stability by ensuring that critical actions such as resource deallocation are performed, regardless of whether an exception is encountered, promoting robustness and fault tolerance .

You might also like