Kotlin Exception Handling Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hi everyone in this video we will learn  about exception handling and kotlin   now the first question here is what is an  exception exception is an abnormal condition means   your code is successfully compiled and you  can execute it but while executing the code   any event that interrupts the normal flow of  your code execution is called exception or   in very simpler word we can say that any error  that occurs in runtime is called an exception   some exceptions are arithmetic exception null  pointer exception index out of bound exception   ioexception etc now these all are classes  and we use these classes to handle exceptions   if required you can also create your own  custom exception class so any exception class   contains three things the first one  is the message that defines the error   then stack trace and we have an optional cause now  let's see how we can handle exception and our code   but before that let's raise an exception now for  example I will write two variables here first one   is val a let's say the value is 10 and i have val  p that is zero(0) now if I will try to divide A   with B then I will get an arithmetic exception  that is divided by zero exception so what   i will do is i will write println and i will write  here a divided by b and let's see what happens.  Now if I run the code you can see our code  crashed and I am getting an arithmetic exception   that is divide by zero now the same way if  I have a list let's say I have a list of   names and let's say I have only one name  inside this list now if I will try to access   names 1 that is not available inside this list  i will get an exception that is called index out   of bound exception so these are exceptions and  we need to handle the exceptions and our code   so to handle the exception what we can do is we  can use try and catch block so the syntax is we   write try like this and we write catch and inside  the parenthesis after the catch keyword we define   a class the exception class that we want to catch  for example let's say this time i want to catch   arithmetic exception like this so here I can put  the code that will generate an exception let's say   I will write here print ln 2 divided by 0(ZERO)  and inside catch block I will print e dot message   now what will happen as our code will not crash  and this time we are getting a message that   is divided by zero so what happens is you create  a try block and inside try block you put the code   that may raise an exception now if any exception  is raised inside the try block the execution of   the try block will stop from that line and you  will immediately go inside catch block so here   basically we are catching the exception now the  code will not crash but the execution will stop   for example here the exception raised now if i  have other lines here let's say hello and hello   one now these two lines will not be executed but  we will come directly inside the catch block.  And here we can handle the exception keys  inside the catch block now if I run the code   you can see we are inside catch block we are  getting the message that is divided by zero   and these two lines are not executing because  the exception is raised at this line now if i   put the exception at the next line now this time  you will see hello because the exception will   be raised at this line in this situation you can  see we are getting hello then inside catch block   and the message now remember whenever you are  using try block you have to use a catch block   so I hope you got the point how you can handle  an exception now there is one more keyword that   is finally now if you are using finally catch  block is optional so either you have to use a   catch block or a finally block now catch  block will only execute when an exception   is raised if no exception happened in your  code then catch block will not be executed   but finally block will be called irrespective  of exception happened or not happened   so what we do is we put some critical code  inside try block that may raise an exception   inside the catch block we handle the exception  case and inside finally block we put some   important code that we have to execute whether  we had an exception or not now you can also use   try and catch as expressions for example we have  two valves a equals to 10 and val b equals to zero   now we can get the result in another val and we  can write try a divided by p now it will return   the result but if there is an exception we have  to handle it inside the catch block so we have   again arithmetic exception and here we can return  0 now we can print the result and we will get the   result or 0 because we are handling the exception  using try and catch you can see we are getting 0.   Now if I will change the value of b to 2 i  will get the actual result you can see here  now if you have to write multiple  lines inside try and catch.  Inside dry block then the last statement is the  return value and same applies for catch block   you can see we are inside try block the  result is 5.Now if I make the b to 0   we will go inside the catch block and the result  is 0.So first we went inside the try block this   line is ok but at this line we got an exception  so we went inside the catch block and we print   inside catch block and we get the return value  that is 0 and we are printing 0 from this line   so this is how you can use try catch  and finally to handle your exceptions   now you can also throw exceptions if  required so let's say I have a function   and to this function i will pass let's say  name of the voter and age of the voter now if   age is less than 18 in this case we can throw  an exception and this time i will throw illegal   argument exception and i will give the  message as younger than 18 cannot vote   now in the else case I can write voted just  for example so to throw an exception we use   this throw keyword and then we create  an instance of our exception class   now you can use any class here and if you want  you can also create your own exception class   so very first we have the throwable class so i  will tell you the class hierarchy first we have   throwable class then we have exception class  and then we have all other exceptions that are   predefined now you can also create your own  exception class for example I can create a class   that is illegal voter exception or you can give  your class any name and i can extend the exception   class here I can pass a message to my class  like this and i can pass the same message to my   exception super class and here i can  use this class if required like this   so right now i am throwing my custom  exception here now if I call this function let's say i will call it here   i am calling vote i will pass my name and let's  say my age is 17. now if i will run the code you can see I am getting the illegal voter  exception and I am getting the message now i can   handle this exception using try and catch like  this and i can pass illegal voter exception   now sometimes if you are not sure which  class to write here you can simply write   exception and here i can print the stack trace   using a predefined function that is print stack  trace now this time i will get the same thing but this time our code is not crashing  as you can see we are getting the   other results as well so that is why we need  to handle the exception now one more thing   you can have multiple catch blocks for example you  are catching multiple exceptions for example you   have two three exceptions that may happen inside  the try block then in that case you can have   multiple catch blocks like this but you  can have only a single finally block so that's it for exception handling  friends I hope you found this video   helpful and learned something and you  learned how you can handle exceptions   and kotlin in case you have any confusion  or questions you can leave your comments   below so thanks for watching everyone  this is BELAL KHAN now signing off.
Info
Channel: Simplified Coding
Views: 1,916
Rating: 5 out of 5
Keywords: kotlin try catch, kotlin exception handling, kotlin programming tutorial, kotlin object oriented programming, kotlin finally, kotlin try, kotlin catch
Id: LaOGjhyODrI
Channel Id: undefined
Length: 12min 22sec (742 seconds)
Published: Fri Feb 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.