Page 121 - Cs_withBlue_J_C11_Flipbook
P. 121

5.5 BASIC CONCEPT OF AN EXCEPTION
                 During the execution of a program, an error may occur, which may stop the execution of the program. This error is
                 known as an exception.

                 A message is generated, which sometimes becomes difficult for the users to understand. In Java, a programmer can
                 edit the messages so that they may be easily understood.


                 5.5.1 Types of Exception
                 There are two types of exceptions in Java, which are as follows:

                 •  Checked exceptions
                 •  Unchecked exceptions

                 Checked Exceptions
                 Except for runtime exceptions, all exceptions are known as checked exceptions as they are checked by the compiler.
                 For example, IOExceptions, ClassNotFoundException, etc.

                 Unchecked Exceptions
                 Exceptions that bypass the compiler are known as unchecked exceptions. They are also known as runtime exceptions.
                 For example, DivideByZero, Arithmetic Exception, etc.

                 5.5.2 Exception Handling
                 When an exception occurs, the program stops immediately with a system-generated message. To provide a user-
                 friendly message, we need to handle the raised exceptions. Using Exception Handling, we can ensure that the flow
                 of the program doesn’t break when an exception occurs. So, exception handling is a mechanism to handle errors that
                 occur during the execution of the program so that the normal flow of the code can be maintained.

                 Java try-catch block
                 The “try” block and “catch” block help to handle the exception raised. In the “try” block, we write the codes that are
                 needed to test for the errors and the “catch” block is used to define those blocks that are going to be executed if any
                 error occurs in the “try” block.
                 Syntax of try-catch block:
                    try {
                      // Block of code to try
                    }
                    catch(Exception e) {
                      // Block of code to handle errors
                    }
                 Java throw statement
                 When the throw statement executes, the nearest catch statement is checked to see what type of execution has been raised.

                 Syntax of throw statement:
                    throw ThrowableInstance;
                 Java throws keyword
                 A method can throw many types of exceptions during the execution of the program. The throws keyword handles all
                 these types of exceptions. This is used to help the programmers with prior knowledge about what types of exceptions
                 are to be handled.
                 Syntax of throws keyword:

                    return_type name_of_method(list_of_parameters) throws exception_list
                    {


                                                                                                                       119
                                                                                                             Objects   119
   116   117   118   119   120   121   122   123   124   125   126