Discover how to create exception in java, include the articles, news, trends, analysis and practical advice about how to create exception in java on alibabacloud.com
When I was learning Java exception handling, I remember a few words about the handling of a parent-child exception. A subclass method can only throw an exception or its child exception that the parent class method throws, and the child class constructor must throw an
The difference between the checked exception and the unchecked exception in Java is described in detail.
(1) Java exception hierarchy
To understand the differences between checked Exception and unchecked
class and its subclasses represent run-time errors, which are typically thrown by Java virtual machines, and the JDK defines some error classes, such as Virtualmachineerror and OutOfMemoryError, The program itself cannot fix these errors. Generally do not extend the error class to create user-defined fault classes. The RuntimeException class represents an error in program code that is extensible and allows
How to make our exception handling flexible enough-we can create an exception class ourselves (common operations for all exceptions)In the user management system, the specific operation is as follows:1. Create a Userexception class2, find superclass, click Browse, select Java.lang.Exception, complete the creation3, ope
constructor with a string, this string will be used as an additional description for this exception object except for the type name.Why create your own exceptions?
When Java built-in exceptions cannot explicitly describe the exceptions, you need to create your own exceptions. It should be noted that the only useful in
When Java programs violate Java semantic rules, the Java Virtual machine will represent errors that occur as an exception
The violation of the semantic rules consists of two cases:
One is a semantic check built into the Java class library, such as an array of bounds that thr
When doing a function of converting a string to integer today, it is found that Integer.parseinte () throws an exception numberformatexception.function integer.parseint (String) definition1 int parseint (String s) 2 Throws NumberFormatExceptionTest code: 1 public class Test 2 { 3 public static void main (string[] args) 4 { 5 Integer num = integer.parseint ("FF" 6 System.out.println (num); 7 } 8 }We know that a
Java-exception details, java-Details
(1) Causes of exceptions
Exception Handling can improve program robustness. The C language uses function return values to handle exceptions. disadvantages of this approach:
1. The returned value may conflict with the existing logic.
2. Poor code readability. The Execution Code is mi
---restore content starts---First of all, I am a small rookie, recently suddenly whim, want to study the Java exception and exception handling, a little bit of understanding, the birds do not want to ridicule ...Since we want to deal with exceptions and exceptions, we need to understand the anomalies first, so what is an exce
In Java, all exceptions have a common ancestor throwable (can be thrown). Throwable specifies the commonality of any problems that can be transmitted through a Java application by using the exception propagation mechanism available in the code.Throwable has two important subclasses: Exception (
. Generally do not extend the error class to create user-defined fault classes. While the RuntimeException class represents an error in the program code, it is extensible and allows the user to create a specific execution-time exception class.The same thing about error (Execution-time error) and execution-time exceptions is that the
First, the basic concept (1) Exception : An abnormal condition that occurs during runtime of a Java program.Java describes and encapsulates an object's behavior in terms of object-oriented thinking. (2) Exception classification : (throwable: Defines the functionality that is common to the problem. )1.Error: From the bottom of the system, JVN tells the user. Do
not been terminated after the division is zero. Thank you !!! ");
}
} Running result:
Hello World !!!The divisor is 0!The program has not been terminated after the division is zero !!!4. runtime error
The Error class and its subclass indicate runtime errors, which are usually thrown by the Java Virtual Machine. Some Error classes are defined in JDK, such as VirtualMachineError.
And OutOfMemoryError, which cannot be fixed by the program itself. Genera
Java Exceptions: Choose checked Exception or unchecked Exception?
Java contains two types of exceptions:
Checked ExceptionAnd
Unchecked Exception。
The difference between checked and unchecked exceptions is that the checked exception
1. There are often many problems when developing (these exceptions are not provided in the Java Internal system framework)For example, the test results must be between 0~100.Obviously Java has no corresponding exception, we need to do an exception ourselves .(1) Inherit from except
will go wrong and the compilation will fail. Of course, both of these exceptions can be caught and processed by a program, such as a zero-divisor run-time exception:Public class HelloWorld {public static void Main (string[] args) {System.out.println ("Hello world!!!");try{System.out.println (1/0);}catch (ArithmeticException e) {System.out.println ("divisor is 0!");}System.out.println ("Divisor is zero after the program did not terminate ah, hehe!!!");}}Operation Result:Hello World!!!Divisor is
and the compilation will fail. Of course, both of these exceptions can be caught and processed by a program, such as a zero-divisor run-time exception:public class HelloWorld {public static void Main (string[] args) {System.out.println ("Hello world!!!");try{System.out.println (1/0);}catch (ArithmeticException e) {System.out.println ("divisor is 0!");}System.out.println ("Divisor is zero after the program did not terminate ah, hehe!!!");}}Operation Result:Hello World!!!Divisor is 0!Divisor is z
Java defines two types of exceptions:- Checked exception: Inherit from Exception class is Checked exception. The code needs to handle the checked exception that the API throws, either with a catch statement or directly with a throws statement.- Unchecked
PackageCom.swift; Public classEXCEPTION_TESTC { Public Static voidMain (string[] args) {/** Question 5th: There is a class ClassA, there is a class of CLASSB, there is a method B in ClassB, this method throws an exception, in the ClassA class there is a * method A, call B in this method, and then throw an exception. In a client that has a class of TESTC, there is a method for C, please catch the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.