Let’s try to implement try-catch okay so what do you mean by try-catch try block contains the code that might throw an exception make sense we don’t write anything extra hit here in try block okay after the exception will get executed if the exception occurred cache block is used to catch the exception thrown by the statement in that try block okay so for example if you can see the example below what I am using so in the for loop if you have defined it’s syntactically correct right.
Its index is correct right and what do you want to do is what you are trying to do right if your what you are trying to do is what you want to do in the for loop then it will work fine now there is an exception that might occur when you’re trying to divide you see a will be divided first by three then a will be divided for a second by two then a will be divided by one then a will be divided by zero so at that time the try block will execute okay so because it’s an exception what the try throws it to the catch okay earth Matic exception right this is an automatic exception when we are trying to divide anything by zero that’s what we get in that then we are trying to print that exception.
We got Java dot Lang dot earth metal exception by zero that dividing by zero we are trying to divide anything by zero so cache block must follow the try block okay else it will give a compile-time error so every time if you’re using try block then catch block must follow it okay cache block is used to catch the exception thrown by the statement in that dry block so this is the program to implement TriMet now I mean to say to implement try-catch how we implement try how we implement catch okay there are multiple there can be multiple caches which should be placed after the child to parent to get the desired output right.
If you are trying to implement multiple caches that can also be done so this must follow for example what will be my child to parent in order right so there should be an order why if you are implementing this catch for example there is a general exception right that is called this exception class that is written here exception e this is the parent class okay now this exception is a child class that automatic exception is the child class of this exception class okay so the exception class that is written in the parent okay parent of every X exception right so if we have written them instead of this earth Matic exception.
We have written exception e then after that the cash after we have written multiple caches the next cache we read we wrote is arithmetic exception e that will give an error because it has to follow child must come first then parent so if we want to implement multiple caches here then the earth Matic exception he should be first in the catch and then automatic then the exception e in the next catch okay this order must follow now finally so this code contains the code which will be executed no matter.
If the exception is thrown or not finally generally contains the code of file release memory cleaning and all those things that the memory release code and memory cleaning these type of codes are done in finally so if there’s an exception that we are again trying to divide 10 by 2 then there is no exception right there is no exception has occurred but the final statement will always execute so there will be an exception there will be no exception in this case right but the final statement will be still executed you see the output I am finally so this is what we use finally for if you want something to be executed no matter if the exit if the exception has occurred or not finally.
We’ll still be executed ok now the throw keyword is used to explicitly throw an exception which normally doesn’t throw an exception so for example if you explicitly that is not automatically herein we were automatically throwing an exception in this case right it automatically throws the exception but if you yourself want to throw an exception that throw is the keyword that we to use for example in this class what we wrote that if the age is less than eighteen right then throw an exception that’s how we do it throw a new exception right in the dry block if the age is less than 80 okay now if it’s not then there will be no exception thrown so if you want explicitly to throw an exception throw is the keyword we use and how we use it to throw a new exception right we use the new keyword here okay we because we are generating an ex exception right.
That’s how we do it now finally we will talk about throws so throw is basically says the sort of ex exception the code can throw and relies on the coiler to handle so herein we are not explicitly throwing an exception rather it is upon the handler the coiler that defines the exception or that throws the exception for example here what we have written try function then we call the function and we pass the argument n in it right and we have also passed an argument 0 in it right so once the function has been called in a there will be 10 first then the system will you 10/10 right there is no exception so no exception is thrown by the function that has been defined.
Now if 0 has been sent now the 0 has been sent in this function right a contains 0 so 10 divided by 0 it’s an exception so it will throw an exception right you want the point right the exception of the code can throw basically throws is responsible for handling the caller right when 0 has been sent so this function is responsible for throwing an exception what we just have to write in front of that function throws exception right then the exception will go in the catch block and can’t divide by 0 is the message that we have we will print ok this is how we the throws works through basically says what sort of exception the core can throw okay and relies on the caller to handle it so the caller defines it and handles it the caller function okay for example here function f F UNC okay this is all about exception handling now the last topic of this session and we are about to conclude this session is the string string why we have defined string here because it is most in most of the programming language string is not datatype right.