1. What is the difference between throw and throws?
throws is used to declare all exception information that a method may throw. throws is to declare the exception but not handle it. Instead, it will upload the exception and let whoever calls me handle it. Throw refers to a specific exception type thrown.
2. What is the difference between final, finally and finalize?
final can modify classes, variables, and methods. Modified class means that the class cannot be inherited, modified method means that the method cannot be overridden, and modified variable means that the variable is a constant and cannot be reassigned.
finally is generally used in try-catch code blocks. When handling exceptions, we usually put the code method that must be executed in the finally code block, which means that the code block will be executed regardless of whether an exception occurs. Generally Used to store some code for closing resources.
(Video tutorial recommendation: java video tutorial)
finalize is a method, belonging to the Object class, and the Object class is the parent class of all classes. The method is generally called by the garbage collector. When we call the System's gc() method, the garbage collector calls finalize() to collect garbage.
3. Which part of try-catch-finally can be omitted?
Answer: catch can be omitted
Reason:
A more strict statement is: try is only suitable for handling runtime exceptions, try catch is suitable for handling runtime exceptions Exception Ordinary Exception. In other words, if you only use try to handle ordinary exceptions without using catch, the compilation will not pass, because the compiler rigidly stipulates that if you choose to catch ordinary exceptions, you must use catch to explicitly declare them for further processing. There is no such provision for runtime exceptions at compile time, so catch can be omitted, and there is nothing wrong with adding the catch compiler.
Theoretically, the compiler is displeased with any code and thinks there may be potential problems, so even if you add try to all the code, the code will only run normally during runtime. Add a layer of skin. But once you add try to a piece of code, you are explicitly promising the compiler to catch the exceptions that may be thrown by this piece of code instead of throwing them upward. If it is a normal exception, the compiler requires that it must be caught with catch for further processing; if it is a runtime exception, it is caught and then discarded and finally cleaned up, or a catch is added for further processing.
As for adding finally, it is a "clean-up" process that must be performed regardless of whether an exception is caught or not.
(recommended related tutorials: java introductory program)
4. In try-catch-finally, if return is made in catch, will finally still be executed? ?
Answer: It will be executed before return.
Code example 1:
/* * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗? */ public class FinallyDemo2 { public static void main(String[] args) { System.out.println(getInt()); } public static int getInt() { int a = 10; try { System.out.println(a / 0); a = 20; } catch (ArithmeticException e) { a = 30; return a; /* * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了 * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40 * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30 */ } finally { a = 40; } // return a; } }
Execution result: 30
Code example 2:
package com.java_02; /* * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗? */ public class FinallyDemo2 { public static void main(String[] args) { System.out.println(getInt()); } public static int getInt() { int a = 10; try { System.out.println(a / 0); a = 20; } catch (ArithmeticException e) { a = 30; return a; /* * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了 * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40 * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30 */ } finally { a = 40; return a; //如果这样,就又重新形成了一条返回路径,由于只能通过1个return返回,所以这里直接返回40 } // return a; } }
Execution result: 40
5. What are the common exception types?
NullPointerException: This exception is thrown when the application attempts to access a null object.
SQLException: Exception that provides information about database access errors or other errors.
IndexOutOfBoundsException: Thrown when indicating that a sorting index (such as sorting an array, string, or vector) is out of range.
NumberFormatException: This exception is thrown when the application attempts to convert a string to a numeric type, but the string cannot be converted to the appropriate format.
FileNotFoundException: This exception is thrown when an attempt to open the file represented by the specified pathname fails.
IOException: This exception is thrown when some kind of I/O exception occurs. This class is a general class for exceptions generated by failed or interrupted I/O operations.
ClassCastException: This exception is thrown when an attempt is made to cast an object to a subclass that is not an instance.
ArrayStoreException: Exception thrown when trying to store an object of the wrong type into an object array.
IllegalArgumentException: An exception thrown indicating that an illegal or incorrect parameter was passed to the method.
ArithmeticException: This exception is thrown when an abnormal operation condition occurs. For example, when an integer is "divided by zero", an instance of this class is thrown.
NegativeArraySizeException: This exception is thrown if the application attempts to create an array with a negative size.
NoSuchMethodException: This exception is thrown when a specific method cannot be found.
SecurityException: Exception thrown by the security manager to indicate a security violation.
UnsupportedOperationException: This exception is thrown when the requested operation is not supported.
RuntimeExceptionRuntimeException: is the super class of exceptions that may be thrown during the normal operation of the Java virtual machine.
If you want to know more about interview questions, please visit java interview questions.
The above is the detailed content of 2020 New Java Interview Questions-Exception. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.