Page 122 - ComputerScience_Class_11
P. 122

5.6 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
              modify the messages so that they may be easily understood.


              5.6.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, IOException, ClassNotFoundException, etc.

              Unchecked Exceptions
              Exceptions that bypass the compiler are known as unchecked exceptions. They are also known as runtime exceptions.
              For example, NullPointerException, ArithmeticException, etc.

              5.6.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 thrown 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 exception has been thrown.
              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 declares 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
                  {



                  120  Touchpad Computer Science (Ver. 3.0)-XI
   117   118   119   120   121   122   123   124   125   126   127