10.1 Exception Handling in Java Theory

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so welcome back aliens this is Robin ready from physical learnings and in this video we'll talk about exceptions you know when it comes to uh programming we are we are concerned about others right normally we need to add a software uh we don't expect output we expect errors right so whenever you write the first code for C programming maybe if you're learning Java if you're learning any language the first concern you have is others right uh if you write a code the first thing you see is an error right example if you have you can you know when you uh declare a variable but when you don't declare a variable and you try to use it when you when you perform all those actions you get errors but in Java we have different types of Errors right so if we talk about errors we have two things here we have errors and exceptions okay so we have two different things so we have errors and we have exceptions so basically what happens is uh when you write a software there might be lots of different kinds of exceptions right example you're trying to open a file and file doesn't exist right so there's an exception uh you are asking for the input from the user and user is not giving the giving you the input so that's an exception you're trying to uh trying to run a process but CPO is not available so that's an exception you're trying to divide a number by zero and that's an exception right so there are different kinds of exceptions so normally this exception is uh you know they have a tree here for the exception so the main class is the turbo class you can see that on the top so so if you talk about the exception hierarchies or the main class is throwable okay that's a catch here normally what happens is whenever you have a able at the end example uh cloneable closable then we have what CLI is able so all this able that interfaces but here this throwable is not interface it's a class okay so the subclass of throwable is exception okay in fact we have two we have exception and errors so difference between exception and errors are so exception we can handle as a user as a programmer we can handle the exception what about errors we cannot handle errors example uh your jvm is not working properly what you can do for that nothing right maybe your your RAM is going out of memory you cannot do anything for that right so errors you cannot handle what about exceptions exceptions are something you can handle right now in exception we have two different types again one is a checked exception and then we have unchecked exceptions again we'll talk about that later but we have two things we have checked and we have unchecked right so let's let's talk about what exactly exception means first and then we'll discuss what is checked and unchecked you know when you talk about the software or uh when you talk about any code you know when you when you build a software there are lots of statements right so the statement can be uh initial initialization statement there can be some operations there may be class definition whether definition there are different type of statements right so we can classify these statements in two types the first type of statements are called as normal statement and the second type of statement is called as critical statement now what exactly it means uh you know when you let's say you your your your you know you have a nephew with you a small small kid with you you're going out somewhere okay and you are you are sitting in a restaurant and you're having something and you're that kids the small kid he's running here and there and that's okay right there will not be any big issue because it's a restaurant maximum what will happen is he will fell down and then that's okay right so but if you're if you're walking on Highway and suddenly you're that kid leaves your hand and running on the highway alone and that's critical right so you will run for run to catch him right so there are two different situations one is normal situation and second is critical situation right in the same way in your software when you write a code we have two double statements normal statement where you will not face any problem then we have critical statement in which you may uh face some problem example if I say eight I equal to one there's no problem with that right we can assign a value of one to an integer variable that's okay okay so that's normal statement but as soon as I say ain't K is equal to I divided by J now this statement becomes critical now you will tell what's what's critical than that the thing is J the value of J might be zero in this situation any number divided by 0 will throw an exception right so that's a critical statement so first you have to classify your normal statement from critical statements okay so you can write the normal statement in normal way but the problem starts when you want to handle the critical statements so whenever you have critical statements always write your critical statements inside a block okay now what that block means is if that blocks is generating any kind of exception it's you you have to handle it okay then question arise how will you handle it so for that we have to use a block which will be called as try block okay so you can see here so we have this statements inside a try block okay if you find any exception if you find any problem there the pointer your your execution will go to catch now what it means so let's say if you if you're right if you return this statement which is I uh in k equal to I divided by J where J is 0 it will throw okay it will throw the exception so now only what happens in Java everything is object right so it will create the what you say it will create the object of exception and it will throw the exception now you have to catch it now how will you catch it so we use a try block and then we use a catch block so this catch block will catch the exception okay so try we'll throw it catch will catch it okay now there are different kinds of exceptions right so this is the this is normal uh what you say the sample we have try we have catch okay so if you are trying uh so this I divided by 0 will so an arithmetic exception so you have to catch an automatic exception but let's say in your block you have another style another type of exception which is maybe IO exception example if you're trying to open a file and then the file is not available it will throw an exception right so required a catch which will accept IO exception but unfortunately we have a catch here which only takes arithmetic right so what we have to do is we have to write one more catch so we can have one try and we can have two catch statements one catch will accept uh which which will work for arithmic exception one catch will work for Io exception let's say in your block you have one more exception again you have to add one more catch there but the problem is let's say there is there is a statement and you don't know what kind of exception it will throw now what so we can go for a general exception called as exception okay exception is a superclass of all the exceptions so if you talk about either exception the super class is exception uh what more automatic exception superclass is other exception then we have array index out of Bounce exception again we have exception so what you can do is if you write exception it will handle everything so what you should do is after this these three catches you should write at the at the end you have to mention exception or catch with exception okay so we can have multiple catch with one try okay so that's how you you mention uh try with catch but again we have some tricks here so once you complete this Theory video this is just a theory of that in the Practical session we'll talk about how to handle the exception how the what what should be the sequence can you can you just write except catch exception on the top is it possible we'll see all those things once you see once you once you start with all the Practical stuff okay we we have one more thing let's say uh we can handle the exception with the help of try catch right so the exception can be of two types it can be catch or it can be un uh so it can be checked or it can be unchecked so when you say the exception is checked it simply means that you you are allowing your user to or if you are allowing your client to handle it okay there is so let's say if this is the way or if this is the way you can actually uh you can actually jump to the different way if you're finding an exception okay but let's say if you if you're if your user cannot do anything they cannot handle the exception make it unchecked don't don't make it compulsory for them to handle it okay so if they can handle it make it checked if they cannot handle it make it unchecked make sense right so let's check then unchecked uh but let's say we have we have three methods here we have method a b and c okay so method a calls method B method B calls method C okay in this scenario if C has an exception so if C is throwing an exception now we have two Choice One see himself can actually handle the Exception by using try catch or what we can do is we can ask see don't solve the Exception by yourself send that exception to B Because B is good at handling the exceptions okay now we have two choice B can handle the exception using try catch or B can throw the exception to a now a will handle it okay so we can also handle the exception using throws so throw simply means you are sending the request about the stack so C calls B to handle the exception b b ask a to handle the exception so we can also use close keyword for that okay in fact we can create our own exceptions using throws keyword or throw keywords so we have throw we have throws we have finally so we'll discuss all this all those stuff when once we start with the Practical implementation so I think that's it that's what his exception is all about uh so make sure you watch the Practical sessions also to make the clear understanding of it uh if you or you can just find the Practical session in the description in the in description area so that's it thanks for watching but do make sure you subscribe the channel so that you can watch uh more videos so that's it thank you so much
Info
Channel: Telusko
Views: 523,699
Rating: undefined out of 5
Keywords: Java Exceptions, Errors, checked exceptions, unchecked exceptions, Run time exceptions, new exceptions, Throwable, throws, throw, try, catch, finally, java programming, java beginners tutorials, software programming, java tutorials, theory, telusko, basic, simple, what is, how to, java, types of error, types of exception
Id: 8WTVLa1Xtsk
Channel Id: undefined
Length: 10min 0sec (600 seconds)
Published: Thu May 19 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.