+ 4
whats the difference between throws exception and try catch
5 Antworten
+ 2
In simple Way
try is used for RISKY CODE where a programmes can think that There may be a chance Of occurring Exeption then Programmer will use try block and if there is any Exception occurs like (Division /O) then it will Throw that exception to CATCH block and Catch is use to varify exception type and Handle It.
#Every try must have atleast 1 Catch or finally or Both..
#Throws block never handle any exception It only Delegate the Exception to Main method or Caller method..
+ 2
Hi,
if an exception occurs inside a function, that is declared to throw an exception in some cases (e.g. public void func() throws exception {...}), this exception will be given to the calling method, instead of being handled within the current method.
If an error occurs in a try-block, it will be given to the catch-block and is handled there (if the error-type is declared in the catch-head).
0
please go through exception handing and multiple exception topic once again. Even after that if you have any issue do point it out.
0
Hello,
Throws exception is not important to throw but catch exception is so import i will give an example to you
If i have a method in example.java and it make divided operation and i didnot male any if coondation here there is an runtime exception because if tge user enter the second number 0 the program will stop because in math we cannot divid on zero so we write throws ....exception next to method declare this is throw and we know that methode block arenot make any thing untill i use it like example();
I will catch the exception when i go to use it in any other place like main i make like that
try{
divided();
}
catch(Exception e)
{
//anything
}
Btw you can catch the method without throw exception but i used in ligcat to know where is the error in your code (in larges apps)
- 1
fjb to the RS