We will extend our discussion about ‘Exceptions’ which we had started in an earlier post. These were the key takeaways from our earlier post:
- Differences between checked and unchecked exceptions
- Important points regarding ‘finally’ clause
- Specific exceptions must be declared before the general exceptions
In this post, we will discuss the legal and illegal forms of ‘try-catch’ constructs and more ways to declare exceptions.
A. Legal and Illegal forms of ‘try-catch’ constructs :
It is illegal to use ‘try-catch-finally’ clause without the ‘catch’ clause or the ‘finally’ clause. In other words, it is illegal to use the ‘try’ clause alone. When the ‘try’ clause is used alone, it will result in compiler error. Let us see an example to illustrate this:
package whizlabs; public class except_2 { public static void main(String[] args) { int i=10,j; // TODO Auto-generated method stub try { j = i/10; } } }
In the above example, the ‘try’ clause is used alone without the ‘catch’ clause or the ‘finally’ clause. This results in a compiler error.
B. The ‘catch’ clause or the ‘finally’clause when used – must be used immediately after the ‘try’ clause. It is illegal to use it after several statements following the ‘try’ clause. The following program illustrates this:
package whizlabs; public class except_2 { public static void main(String[] args) { int i=10,j; // TODO Auto-generated method stub A: try{ j = i/0; } System.out.println("Division by zero causes an exception"); B: catch(ArithmeticException e){} } }
The above program shows the presence of the following statement
“System.out.println(“Division by zero causes an exception”);”
placed between point ‘A’ and point ‘B’(which is in between the ‘try-catch’) results in a compiler error.
Consider this for a legal version of the ‘try-catch’ construct:
package whizlabs; public class except_2 { public static void main(String[] args) { int i=10,j; // TODO Auto-generated method stub try{ j = i/0; } catch(ArithmeticException e){ System.out.println("Division by zero causes an exception"); } } }
This will result in the following output:
“Division by zero causes an exception”
C. Extending on point ‘a’, the ‘try’ clause can have either the ‘catch’ clause or the ‘finally’ clause or both. This is also a legal example in addition to the above example :
package whizlabs; public class except_2 { public static void main(String[] args) { int i=10,j; // TODO Auto-generated method stub try { j = i/10; } finally{ System.out.println("We are done"); } } }
In the above example, we see the ‘try’ clause used along with the ‘finally’ clause.
The output of the above program as expected will be as follows:
We are done
Declaring exceptions:
We saw the legal and illegal forms of ‘try-catch’ constructs. Next we will see how to declare exceptions. Consider the following example:
package whizlabs; import java.io.*; public class except_2 { public void except() throws IOException { except_example(); } void except_example() throws IOException{ } }
The above example shows the way to throw an exception. In order to throw an exception, we use the ‘throws’ keyword along with the type of exception.
In the above example, the methods ‘except()’ and ‘except_example()’ throw ‘IOException’. Declaring a method throws an exception, is just an indication that it might throw an exception – it is not a necessity that it must throw the exception.
In the above example, it can also be noted that both methods are throwing the same exception. Any guesses on why it is so? We will discuss this and other things in yet another blog post.
Bibliography
SCJP 6. In B. B. Kathy Sierra.
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021