0% found this document useful (0 votes)
4 views4 pages

Exception Handling 1742358615

The document explains exception handling in Java, defining exceptions as unexpected breaks in program flow that can lead to crashes. It details the use of try-catch blocks for handling exceptions, the difference between errors and exceptions, and the concepts of checked and unchecked exceptions. Additionally, it covers specific exceptions like RuntimeException and OutOfMemoryError, along with differences between ClassNotFoundException and NoClassDefFoundError.

Uploaded by

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

Exception Handling 1742358615

The document explains exception handling in Java, defining exceptions as unexpected breaks in program flow that can lead to crashes. It details the use of try-catch blocks for handling exceptions, the difference between errors and exceptions, and the concepts of checked and unchecked exceptions. Additionally, it covers specific exceptions like RuntimeException and OutOfMemoryError, along with differences between ClassNotFoundException and NoClassDefFoundError.

Uploaded by

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

Exception Handling

1. What is an Exception?
➔ It is an unexpected break in the program's flow that can cause the program to crash.

2. How the exception is handled in java?


➔ In Java, exceptions are handled using a "try-catch" block, where the code that might
throw an exception is placed within the "try" block, and the "catch" block contains
the code to handle the exception.

3. Difference Between Error and Exception in Java?


➔ Error: It is a mistake done in a code by the programmer.
➔ Exception: It is a unexpected break in the program’s flow, that can cause the
program to crash.

4. Can we keep other statements in between try, catch and finally blocks?
➔ No. Between the try, catch, and finally blocks, no further statements should be
written. They work together as a single entity.
5. Can we write try block without catch and finally block?
➔ No, it is not mandatory that each try block must be followed by a catch block in Java.

6. There are Three Statements in try block – Statement1, Statement2, Statement3. After
that there is a catch block to catch the exceptions occurred in the try block. Assume
that exception has occurred in statement2, Does the statement3 get executed or not?
➔ No. If the statement2 is having exception within try block then the statement3 will
not get executed.
7. What is unreachable catch block error? Explain the hierarchy of exceptions in Java?
➔ A block of statements to which the control can never reach under any case can be
called as unreachable blocks.

i.e., In a catch, if we mentioned Exception and, in another catch, we cannot able to


mention the type of Exception because the Exception is parent of
NoSuchElementException. Either we should use NoSuchElementException first, before
Exception or we should not use NoSuchElementException once we declared the
Exception in catch block.

8. What is Runtime Exception in Java and give example?


➔ In Java, a RuntimeException is a type of unchecked exception that occurs during the
execution of a program.

Common Examples of RuntimeException:

• NullPointerException: Attempting to access or modify a null object.


• ArrayIndexOutOfBoundsException: Trying to access an index of an array that is
out of bounds.
• ArithmeticException: Performing illegal arithmetic operations, like dividing by
zero.
9. what is out of memory error in java?
➔ In Java, an OutOfMemoryError is a serious runtime error that occurs when the Java
Virtual Machine (JVM) is unable to allocate enough memory to continue execution.
This usually happens when the heap, stack, or other memory areas are exhausted.

10. What are Checked and Unchecked exception in Java?


➔ Checked Exception: The Exception that are checked at Compile Time. The java
compiler forces the developer to handle these exceptions using try-catch or declare
them in the method signature using throws.
➔ Unchecked Exception: Also called as Runtime Exceptions, this will not-check at
compile time, These occur due to programming logic errors and do not require
explicit handling.

11. Difference between ClassNotFoundException and NoClassDefFoundError in java?


➔ Both ClassNotFoundException and NoClassDefFoundError in Java are related to
missing classes, but they occur in different scenarios.
ClassNotFoundException NoClassDefFoundError
1. The requested class file is not ➔ The class was compiled
available in the classpath. successfully but deleted or
moved before runtime.
2. The class name is misspelled while ➔ A dependency issue in a JAR file
loading it dynamically. prevents the class from loading.
The class depends on another
class that is missing.

You might also like