Banner_Ads

Interview Question-Answers | Tutorial For Beginners


tech4allsa2z:This blog provides you tutorial and most common Interview questions for beginners related to the following technology like java, python.

Java Exception Handling Question and Answers

 


 Q-1: What is an exception?    

Exceptions are abnormal conditions that occurs during the program execution and distrupt the normal flow of the program. It may occur due to wrong user input or may be wrong logic written by programmer.

Q-2: What is an exception handling in java?

Exception Handling is a mechanism to handle the exceptions that occurs during runtime execution such as ClassNotFoundException, IOException, SQLException, etc..

Q-3: Exceptions are defined in which java package? OR which package has definitions for all the exception classes?


This package contains definitions for Exceptions

Java.lang.Excaption

Q-4: What are the different types of exceptions in java?

There are two types of exceptions:
Checked exceptions (Compile time Exception): These exceptions occur during the compile time.it check the logic is correct or not. Must be handle by the programmer.
Unchecked exceptions (Runtime Exception): These exceptions occur at runtime.it checks the resources are available or not.eg. FileNotFoundException

Q-5: What is the difference between Error and Exception?

Error: Known as catastrophic failure. We can’t write a program to handle the Errors.

 e.g. Power Failure, JVM out of memory etc.

Exception: An abnormal condition that occurs during the program execution. We can write the program to handle the exceptions.

Q-6: What is throw keyword in exception handling?

The throw keyword is used for throwing user defined or pre-defined exception forcefully.

Q-7: What is throws keyword?

If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method’s signature. Throws keyword is used to throw multiple exception at a time.

Q-8: Difference between throw and throws in Java

The throw keyword is used for throwing user defined or pre-defined exception forcefully. Using throw we can throw only one exception at a time. Throw keyword is use within the method.

The throws keyword also used for throwing user defined or pre-defined exception forcefully. Using throws we can throw multiple exception at a time. Throw keyword is use with the method signature.

Q-9: Can static block throw exception?

Yes, A static block can throw exceptions. It has its own limitations: It can throw only Runtime exception (Unchecked exceptions), In order to throw checked exceptions, you can use a try-catch block inside it.

Q-10: What is finally block?

 Finally block is a block of code that always executes, whether an exception occurs or not. It also known as must executable block. Finally block always followed by try or try-catch block.

Q-11: Can we have a try block without catch or finally block?

No, we cannot have a try block without catch or finally block. We must use try block with catch or finally block.

Q-12: Can we have multiple catch blocks following a single try block?

Yes, we can use multiple catch block in order to handle more than one exception.

Q-13: Is it possible to have finally block without catch block?

Yes, we can have try block followed by finally block without even using catch blocks.

Q-14: When a finally block does not get executed?

If you call System.exit() function or if the JVM crashes.

Q-15: Can we handle more than one exception in a single catch block?

Yes, we can. We should have one catch block for one exception.

FAQs

Q: Can we have multiple catch blocks for a single try block in Java?

A: Yes, multiple catch blocks can handle different types of exceptions in a single try block.

Q: Is it necessary to use a finally block after every try-catch block?

A: No, the finally block is optional. It executes whether an exception is caught or not.

Q: What is the purpose of the throws keyword in Java?

A: The throws keyword is used in a method signature to declare the exceptions that might be thrown by that method.

Q: How can custom exceptions be created in Java?

A: Custom exceptions can be created by extending the Exception class or its subclasses.

Q: Can we catch errors in Java using a try-catch block?

A: No, errors are not meant to be caught and handled by application code.


👉Core Java Interview Questions and Answers for Freshers 

👉Important Core Java Interview questions and answers

👉Oops Interview Questions in Java for Freshers

👉Java Collection Framework Interview questions for Freshers

👉Java9 Interview Questions & Answers for beginners

👉Java8 Interview Questions & Answers for beginners 


Post a Comment

0 Comments