Presentation is loading. Please wait.

Presentation is loading. Please wait.

TRY CATCH BLOCK By Kosala Rajapaksha.

Similar presentations


Presentation on theme: "TRY CATCH BLOCK By Kosala Rajapaksha."— Presentation transcript:

1 TRY CATCH BLOCK By Kosala Rajapaksha

2 Program Errors Syntax Exceptions Logical Errors Errors
By Kosala Rajapaksha

3 Syntax Errors(Compile Time Errors)
Generated because of an invalid syntax. Compiler shows those. Program never compile. Exceptions(Run Time Errors) These type of errors generated while program is running. Compiler can’t detect those. So program is successfully compiled. Logical Errors(Semantic Errors) The program is compiled and run. But it generate unexpected results because of invalid logics. Also known as bugs. By Kosala Rajapaksha

4 Try Catch Block is used to handle Exceptions.
{ //statements which can generate Exceptions. } catch(Exception x) //statements to process the exception. By Kosala Rajapaksha

5 Example public class Divide { public static void main(String[] ar)
int x=10; int y=0; try int z=x/y; System.out.println(z); } catch(ArithmeticException ex) System.out.println(“devide by 0”); By Kosala Rajapaksha

6 Ex:-ArithmeticException ArrayIndexOutOfBoundsException
Exception Types There are inbuilt child classes inherited from the Exception base class to catch specified exceptions. Ex:-ArithmeticException ArrayIndexOutOfBoundsException NumberFormatException NullPointerException ClassNotFoundException IOException FileNotFoundException You can declare your own Exception class inherited from Exception Class. By Kosala Rajapaksha

7 Concern There can be multiple catch statements with a single try block. Can’t be a catch without a try. Propagate the exception is also another method to handle it. But this is not a good technique. By Kosala Rajapaksha

8 Finally Block Placed after the try catch block. The code inside will execute either exception generate or not. public void Method1{ Try { // some code(Possible to generate an exception) } Catch(Exception x) { // some code(Process the exception) Finally //this code will be executed whether or not an exception is thrown or caught By Kosala Rajapaksha

9 THANK YOU


Download ppt "TRY CATCH BLOCK By Kosala Rajapaksha."

Similar presentations


Ads by Google