Рет қаралды 204,489
In this video of code decode we have covered all important Exception Handling Interview questions in Java for freshers and experienced
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
openinapp.co/u...
Course Description Video :
yt.openinapp.c...
Exception Handling part 2 : • Exception handling Int...
Q) What is an exception?
The exception is an abnormal condition that occurs during the execution of a program and disrupts the normal flow of the program. If not handled properly it can cause the program to terminate abruptly.
Q) How do we handle exceptions in Java
Try
Encloses set of statements which can throw exception hence are required to be monitored.
Catch
When exception occur, this block catches that exception and work accordingly to handle it or to throw it as required.
Finally
This block gets executed always regardless of exception occurrence. Hence clean up is done here.
Q) Difference between Exception VS Error
Exception
We can recover from exception using try catch block or using throw
Compiler will have knowledge about checked Exceptions hence Compiler will force you to use try-catch blocks
Exceptions are related to application
Exceptions include both checked as well as unchecked type.
Exceptions in java are of type java.lang.Exception.
Error
Recovering from Error is not possible
Compiler will not have any knowledge about unchecked exceptions and Errors
Errors are related to environment where application is running
All errors in java are unchecked type
Errors in java are of type java.lang.Error.
Q) Can we write only try block without catch and finally blocks?
No. either catch or finally is must.
if no then what error will come?
Answer : compile time error saying “insert finally to complete try statement” like this:
Q) Can we write any other statements between try catch or finally block?
No. Try must be followed directly by either catch or finally.
Q) Does remaining statements in try block executes after exception occurs.
No. if exception occurs at a particular point in try block then all statements after that statement where exception is occurred will not be execute and the flow goes directly to either catch block if there is any or else program terminates. Hence we need finally block to do all clean up like closing files or removing locks.
Q) What Is the Difference Between Throw and Throws Keywords in Exception Handling in java?.
Throw
Java throw keyword is used to explicitly throw an exception.
Checked exception cannot be propagated using throw only.
Throw is used within the method.
You cannot throw multiple exceptions.
Throws
Java throws keyword is used to
declare an exception.
Checked exception can be propagated with throws.
Throws is used with the method signature.
You can declare multiple exceptions.
Q) What Happens When an Exception Is Thrown by the Main Method?
When an exception is thrown by main() method, Java Runtime terminates the program and prints the exception message and the stack trace in-system console.
Q) What do you understand by unreachable catch block error.
This error comes when you keep super classes first and sub classes later. Like here We kept Exception which is parent of NullPointer Exception first.
Hence the order of catch blocks must be from most specific to most general ones.
-------------------------------------------------------------------------------------------------------------------------------------
Code Decode Playlists
Most Asked Core Java Interview Questions and Answers : • Core Java frequently a...
Advance Java Interview Questions and Answers : • Advance Java Interview...
Java 8 Interview Questions and Answers : • Java 8 Interview Quest...
Hibernate Interview Questions and Answers : • Hibernate Interview Qu...
Spring Boot Interview Questions and Answers : • Advance Java Interview...
Angular Playlist : • Angular Course Introdu...
GIT : • GIT
-------------------------------------------------------------------------------------------------------------------------------------
Subscriber and Follow Code Decode
Subscriber Code Decode : www.youtube.co...
Linkedin : / codedecodeyoutube
Instagram : / codedecode25
--------------------------------------------------------------------------------------------------------------------------------------
#ExceptionHandlingInterviewQuestionsInJava
#ExceptionHandlingHandlingInterviewQuestionsForExperiencedInJava
#javainterviewquestionsandanswers #codedecode