Review Which describes the errors caused by a program and external circumstances which can be caught and handled?
Kinh Nghiệm Hướng dẫn Which describes the errors caused by a program and external circumstances which can be caught and handled? 2022
Hoàng Tiến Dũng đang tìm kiếm từ khóa Which describes the errors caused by a program and external circumstances which can be caught and handled? được Cập Nhật vào lúc : 2022-09-22 04:26:08 . Với phương châm chia sẻ Kinh Nghiệm Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi đọc tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Admin lý giải và hướng dẫn lại nha.5.2.1. Introduction
Nội dung chính- What do you mean by exception handling?Which of these handles the exception when no catch is used?In which of the following An exception error can occur?What is an exception explain various causes of exceptions with the help of a program explain how exceptions are handled in Java?
Runtime errors occur while a program is running if the environment detects an operation that is impossible to carry out.
Example:
If you access an array using an index out of bounds, your program will get a runtime error with an ArrayIndexOutOfBoundsException. To read data from a file, you need to create a Scanner object using new Scanner (new File(filename)). If the file does not exist, your program will get a runtime error with a FileNotFoundException.
In Java, runtime errors are caused by exceptions. An exception is an object that represents an error or a condition that prevents execution from proceeding normally. If the exception is not handled, the program will terminate abnormally.
5.2.2. Exception Handling
Java’s exception-handling model is based on three operations as declare exception, throw exception, and catch exception.
The exception-handling contains a try block and a catch block. The try block contains the code that is executed in normal circumstances. The catch block contains the code that is executed when the statements in the try block is not evaluated. The try block contain a throw method that takes the statements in the try block to catch block.
5.2.2.1. Declaring Exceptions
In Java, this is declaring an exception explicitly in the method header so that the caller of the method is informed of the exception. To declare an exception in a method, use the throws keyword in the method header.
Example:
public void myMethod() throws IOException
The throws keyword indicates that myMethod might throw an IOException. If the method might throw multiple exceptions, add a list of the exceptions, separated by commas, after throws:
public void myMethod()
throws Exception1, Exception2, …, ExceptionN
5.2.2.2. Throwing Exceptions
It is an exception that detects an error to create an instance of an appropriate exception type and throw it.
Example:
Suppose the program that detects an error when a number is divided by zero and throw it.
throw new ArithmeticException (“Divisor cannot be zero.”);
5.2.2.3. Catching Exceptions
When an exception is thrown, it can be caught and handled in a try-catch block, as follows:
try
statements; // Statements that may throw exceptions
catch (Exception1 exVar1)
handler for exception1;
.
.
.
catch (ExceptionN exVarN)
handler for exceptionN;
If no exceptions arise during the execution of the try block, the catch blocks are skipped, unless it skips the try-block and starts the process of finding the code to handle the exception. The code that handles the exception is called the exception handler. Each catch block is examined in turn, from first to last. If so, the exception object is assigned to the variable declared, and the code in the catch block is executed. If no handler is found, Java exits this method, passes the exception to the method that invoked the method, and continues the same process to find a handler. If no handler is found in the chain of methods being invoked, the program terminates and prints an error message on the console. The process of finding a handler is called catching an exception.
Example:
Write a java program that displays the quotient of any integer.
Output:
In general, a template for a try-throw-catch block may look like this:
Advantages of Exception Handling
- It enables a method to throw an exception to
its caller. The caller can handle this exception. Without this capability, the called method itself must handle the exception or terminate the program. Often the called method does not know what to do in case of error. This is typically the case for the library methods. The library method can detect the error, but only the caller knows what needs to be done when an error occurs.To separate the detection of an error (done in a called method) from the handling of an error (done in the
calling method).
5.2.3. Types of Exception
There are many predefined exception classes in the Java API. They are shown in figure below.
The Throwable class is the root of exception classes. All Java exception classes inherit directly or indirectly from Throwable. You can create your own exception classes by extending Exception or a subclass of Exception. The exception classes are classified into three major types. They are
System Errors – are thrown by the JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully.
Exceptions – are represented in the Exception class, which describes errors caused by your program and by external circumstances. These errors can be caught and handled by your program.
Runtime Exceptions – are represented in the RuntimeException class, which describes programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Runtime exceptions are generally thrown by the JVM.
RuntimeException, Error, and their subclasses are known as unchecked exceptions. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and giảm giá with them.
In most cases, unchecked exceptions reflect programming logic errors that are unrecoverable.
Example:
- A NullPointerException is thrown if you access an object through a reference variable before an object is
assigned to it;An IndexOutOfBoundsException is thrown if you access an element in an array outside the bounds of the array. These are logic errors that should be corrected in the program. Unchecked exceptions can occur anywhere in a program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate that you write code to catch or declare unchecked exceptions.
5.2.4. The finally Clause
Occasionally, you may want some code to be executed regardless of whether an exception occurs or is caught. Java has a finally clause that can be used to accomplish this objective.
The syntax:
The code in the finally block is executed under all circumstances, regardless of whether an exception occurs in the try block or is caught. Consider three possible cases:
- If no exception arises
in the try block, finalStatements is executed, and the next statement after the try statement is executed.If a statement causes an exception in the try block that is caught in a catch block, the rest of statements in the try block are skipped, the catch block is executed, and the finally clause is executed. The next statement after the try statement is executed.If one of the statements causes an exception that is not caught in any catch block,
the other statements in the try block are skipped, the finally clause is executed, and the exception is passed to the caller of this method.
The finally block executes even if there is a return statement prior to reaching the finally block.
A common use of the finally clause is in I/O programming. To ensure that a file is closed under all circumstances, you may place a file closing statement in the finally block.
Post a Comment