Explain the Two types of exceptions checked and unchecked with examples.
Checked exceptions are the exceptions that are checked at compile time. These type of exception requires being caught and handled during compile time.
Checked Exception in Java – All exceptions which are direct sub-class of Exception class but does not inherit RuntimeException are Checked Exception
When to use Checked Exception?
Some Examples of Checked Exceptions in Java – IOException, SQLException, DataAccessException, ClassNotFoundException, FileNotFoundException
Code Example: A very common example is the FileNotFoundException
import java.io.*; class Main { public static void main(String[] args) { FileReader file = new FileReader("C:\\test\\test.txt"); BufferedReader fileInput = new BufferedReader(file);
// Print first line of file "C:\test\test.txt" System.out.println(fileInput.readLine());
fileInput.close(); } } |
Result: This program does not compiles and throws a FileNotFoundException (checked exception). |
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type FileNotFoundException
Unhandled exception type IOException
Unhandled exception type IOException
at test.Main.main(Main.java:7)
Point to Note: If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.
Unchecked Exceptions are the exceptions that are not checked at compiled time.
Some Examples of Unchecked Exceptions in Java: Arithmetic Exception, NullPointerException
Code Example: Consider the following Java program. It compiles fine, but it throws ArithmeticException when run. The compiler allows it to compile, because ArithmeticException is an unchecked exception.
class Main { public static void main(String args[]) { int x = 0; int y = 10; int z = y/x; } } |
Result:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.Main.main(Main.java:7)
Explain the Two types of exceptions checked and unchecked with examples.
Explain the types of companies and the exceptions of corporate veil. Discuss the duties of the directors of a company. Give your view on the exceptions of corporate veil and duties of the directors ASSIGNMENT QUESTION PURPOSE The purpose of this assignment is to enhance learners' ability to explain the types of companies and exceptions of corporate veil. Learners are also expected to be able to discuss the duties of directors of a company. REQUIREMENT Explain the types of companies...
a) List common examples of exceptions and explain how exceptions are handles in c++. (try, catch, throw) b) Declare an abstract class Fraction that can be used for arithmetic of the * operator.
With examples, explain the two types of signals (Analog and Digital) and two types of data (Analog and Digital). With the above combinations, provide the real life applications and examples for the communication of the above four combinations. (10 marks)
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...
Explain the concept of privity of contract and two exceptions to this legal concept.
identify and explain with examples Four types of models in research?
Explain and discuss four types of accounting adjustments with examples
Give two examples of types of life cycle transitions.
Define Asset, Liabilities and Equity and give two examples of each of these account types. 2. Define Normal Balance, list the 6 types of accounts mentioned in class (AEDLRC), and list the normal balance for each of those 6. 3. Create a sample T Account with several debit and credit amounts and then calculate the balance of the account and explain the balancing process.
: Define and give examples of three types of corporate mergers, and explain the role of leveraged buyouts and taking a firm private.