diff --git a/2.try-catch-finally/Doc.md b/2.try-catch-finally/Doc.md new file mode 100644 index 0000000..99670a2 --- /dev/null +++ b/2.try-catch-finally/Doc.md @@ -0,0 +1,19 @@ +#Handling Exceptions +####● Two ways of handling exception +#####○ try-catch-finally +#####○ throws keyword +#####● Try-catch-finally handles exception where it occurs + + + +#Try-Catch-Finally +####Consists of Three parts +######1.Try Block +######Risky codes goes here eg: System.out.println(5/0); +######2.Catch block +###### Handler code goes here eg: System.out.println(e.getMessage()); +######3.Finally Block +######This will executed even though exception occurred or not Only JVM shutdown stop executing this part + +#####● Try block can’t exist without catch block or finally block +#####● Catch or finally blocks cannot exist without try block \ No newline at end of file diff --git a/2.try-catch-finally/Sample.java b/2.try-catch-finally/Sample.java new file mode 100644 index 0000000..ecbedd0 --- /dev/null +++ b/2.try-catch-finally/Sample.java @@ -0,0 +1,17 @@ +public class Sample { + public static void main(String[] args){ + try + { + System.out.println(5/0); + + } + catch (Exception e){ + System.out.println(e.getMessage()); + } + finally { + System.out.println("Final code"); + } + } + +} + diff --git a/try-catch-finally/Doc.md b/try-catch-finally/Doc.md new file mode 100644 index 0000000..99670a2 --- /dev/null +++ b/try-catch-finally/Doc.md @@ -0,0 +1,19 @@ +#Handling Exceptions +####● Two ways of handling exception +#####○ try-catch-finally +#####○ throws keyword +#####● Try-catch-finally handles exception where it occurs + + + +#Try-Catch-Finally +####Consists of Three parts +######1.Try Block +######Risky codes goes here eg: System.out.println(5/0); +######2.Catch block +###### Handler code goes here eg: System.out.println(e.getMessage()); +######3.Finally Block +######This will executed even though exception occurred or not Only JVM shutdown stop executing this part + +#####● Try block can’t exist without catch block or finally block +#####● Catch or finally blocks cannot exist without try block \ No newline at end of file diff --git a/try-catch-finally/Sample.java b/try-catch-finally/Sample.java new file mode 100644 index 0000000..ecbedd0 --- /dev/null +++ b/try-catch-finally/Sample.java @@ -0,0 +1,17 @@ +public class Sample { + public static void main(String[] args){ + try + { + System.out.println(5/0); + + } + catch (Exception e){ + System.out.println(e.getMessage()); + } + finally { + System.out.println("Final code"); + } + } + +} +