Exception Handling Interview questions and answers in Java | Part -1 | Code Decode

preview_player
Показать описание
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:

Course Description Video :

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.

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

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

-------------------------------------------------------------------------------------------------------------------------------------
Subscriber and Follow Code Decode

--------------------------------------------------------------------------------------------------------------------------------------

#ExceptionHandlingInterviewQuestionsInJava
#ExceptionHandlingHandlingInterviewQuestionsForExperiencedInJava
#javainterviewquestionsandanswers #codedecode
Рекомендации по теме
Комментарии
Автор

Java 7 and beyond, try with resources allows to skip writing the catch as well as finally and closes all the resources being used in try-block itself.

Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

See this code example

static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
return br.readLine();
}
}

kanchanyasita
Автор

great explanation cleared all the doubt and also gained confidence

aasimahsan
Автор

Please upload videos on advance exception handling and try with reassures as well. Your videos are great for interview preps. Thanks

prisha
Автор

Your explanation is amazing...to the point...understandable...keep making such videos...😊

sandhyarajkumar
Автор

Great content...Upto the point.... Looking for many more to come on the channel 👏

rishikachowdarypotturu
Автор

Outstanding work done... Watching all your videos for learning. Expecting more soon.. Good Job

vibhorgoel
Автор

Hello Mam, You made Java Developer's life easy, , Thank you so much for your dedication towards Java Developer ✌️🙏🏻.. God Bless you 😊

divsreviews
Автор

Many thanks for making such wonderful videos and this really help people to Crack an interview. Please make videos on exception handing with real time scenarios along with garbage collector.

mohantyfamily
Автор

Your explanation is really very helpful ❤️
Thank you so much

prikansiyadav
Автор

Thank you ma'am, was waiting for this video...

shubhamtagalpallewar
Автор

Very Underrated youtuber. Deserves more subscribers.

reddaboinavasista
Автор

Thank you for such good videos.
I have watched your other java videos which came out useful for me

aayushiagarwal
Автор

Advanced exception handling will be helpful

prithvisingh
Автор

Please continue this exception handling series

ayushman_
Автор

Great content...They r really helpful...Concept is very clear...U r sharing ur knowledge with us...It is appreciable madam...Thanks alot ...pls upload advanced exception handling interview questions

mnishi
Автор

very nice and awesone explanation.👌👌👌....keep doing more n more videos like this to u...

aaaaaaaa
Автор

Hi Team CodeDecode! hope you are fine. excellent video as usual. Please make a video on try with resources and exception chaining!

sukunkusum
Автор

Very nice and clear explanation Mam...Its very helpful...Keep it up.

chenchaiahmalli
Автор

Wonderful video mam 🤩🤩🤩🤩🤩. Mam plz do intermediate exception handling questions
too.

arlynsneha
Автор

this video is very helpful and as you said you need to extend this video to intermediate and advanced level.

praveenraj