Page 386 - CITS - Computer Software Application -TT
P. 386
COMPUTER SOFTWARE APPLICATION - CITS
These exceptions are propagated using the throws
6 These are automatically propagated.
keyword.
It is required to provide the try-catch and try-finally In the case of unchecked exception it is not
7
block to handle the checked exception. mandatory.
Bugs or errors that we don’t want and restrict the normal execution of the programs are referred to as exceptions.
ArithmeticException, ArrayIndexOutOfBoundExceptions, ClassNotFoundExceptions etc. are come in the
category of Built-in Exception. Sometimes, the built-in exceptions are not sufficient to explain or describe certain
situations. For describing these situations, we have to create our own exceptions by creating an exception class
as a subclass of the Exception class. These types of exceptions come in the category of User-Defined Exception.
Java try-catch block
Java try block
Java try block is used to enclose the code that might throw an exception. It must be used within the method.
If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So,
it is recommended not to keep the code in try block that will not throw an exception.
Java try block must be followed by either catch or finally block.
Syntax of Java try-catch
1 try{
2 //code that may throw an exception
3 }catch(Exception_class_Name ref){}
Syntax of try-finally block
1 try{
2 //code that may throw an exception
3 }finally{}
Java catch block
Java catch block is used to handle the Exception by declaring the type of exception within the parameter. The
declared exception must be the parent class exception ( i.e., Exception) or the generated exception type. However,
the good approach is to declare the generated type of exception.
The catch block must be used after the try block only. You can use multiple catch block with a single try block.
373
CITS : IT&ITES - Computer Software Application - Lesson 101 - 108 CITS : IT&ITES - Computer Software Application - Lesson 101 - 108