e an exception will occur If we write programs in this way then they will be

E an exception will occur if we write programs in

This preview shows page 25 - 27 out of 56 pages.

terminated abnormally and user who is executing our program or application will not be happy. This occurs because input of user is not valid so we have to take a preventive action and the best thing will be to notify the user that it is not allowed or any other meaningful message which is relevant according to context. You can see the information displayed when exception occurs it includes name of thread, file name, line of code (14 in this case) at which exception occurred, name of exception (ArithmeticException) and it's description('/ by zero'). Note that exceptions don't occur only because of invalid input only there are other reasons which are beyond of programmer control such as stack overflow exception, out of memory exception when an application requires memory larger than what is available. Java provides a powerful way to handle such exceptions which is known as exception handling. In it we write vulnerable code i.e. code which can throw exception in a separate block called as tryblock and exception handling code in another block called catchblock. Following modified code handles the exception. Java exception handling example classDivision { publicstaticvoidmain(String[] args) { inta, b, result; Scanner input = newScanner(System.in); System.out.println("Input two integers"); a = input.nextInt(); b = input.nextInt(); // try blocktry{ result = a / b;
Background image
System.out.println("Result = " + result); } // catch blockcatch(ArithmeticException e) { System.out.println("Exception caught: Division by zero."); } } } Whenever an exception is caught corresponding catch block is executed, For example above code catches ArithmeticException only. If some other kind of exception is thrown it will not be caught so it's the programmer work to take care of all exceptions as in our try block we are performing arithmetic so we are capturing only arithmetic exceptions. A simple way to capture any exception is to use an object of Exception class as other classes inherit Exception class, see another example below: classExceptions { publicstaticvoidmain(String[] args) { String languages[] = { "C", "C++", "Java", "Perl", "Python" }; try{ for(intc = 1; c <= 5; c++) { System.out.println(languages[c]); } } catch(Exception e) { System.out.println(e); } } } Output of program: C++ Java Perl Python java.lang.ArrayIndexOutOfBoundsException: 5 Here our catch block capture an exception which occurs because we are trying to access an array element which does not exists (languages[5] in this case). Once an exception is thrown control comes out of try block and remaining instructions of try block will not be executed. At compilation time syntax and semantics checking is done and code is not executed on machine so exceptions can only be detected at run time. Finally block in Java Finally block is always executed whether an exception is thrown or not.
Background image
Image of page 27

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture