From c1039f929cafab1357202d09d61cd9cdb0718e1d Mon Sep 17 00:00:00 2001 From: ashan-tharaka Date: Sat, 9 Oct 2021 07:02:16 +0530 Subject: [PATCH 1/2] Added exception-handling with try-catch-finally sample code and documentation --- try-catch-finally/Doc.md | 19 +++++++++++++++++++ try-catch-finally/Sample.java | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 try-catch-finally/Doc.md create mode 100644 try-catch-finally/Sample.java 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..dc83d7e --- /dev/null +++ b/try-catch-finally/Sample.java @@ -0,0 +1,16 @@ +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"); + } + } + +} From 1f989220ae1aed2e0d9b0cab54af1e823e6b64a1 Mon Sep 17 00:00:00 2001 From: ashan-tharaka Date: Sat, 9 Oct 2021 07:16:00 +0530 Subject: [PATCH 2/2] Added exception-handling with try-catch-finally sample code and documentation --- 2.try-catch-finally/Doc.md | 19 +++++++++++++++++++ 2.try-catch-finally/Sample.java | 17 +++++++++++++++++ try-catch-finally/Sample.java | 1 + 3 files changed, 37 insertions(+) create mode 100644 2.try-catch-finally/Doc.md create mode 100644 2.try-catch-finally/Sample.java 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/Sample.java b/try-catch-finally/Sample.java index dc83d7e..ecbedd0 100644 --- a/try-catch-finally/Sample.java +++ b/try-catch-finally/Sample.java @@ -14,3 +14,4 @@ public static void main(String[] args){ } } +