Introduction to exceptions and errors in Java learning, catching exceptions, throwing exceptions, custom exceptions (small records in Java learning) Wang Coli (Star stars)
Exception: ( API Java.lang has an exception, there are a lot of )
In the Java program also will be abnormal situation, this is called an exception.
Java is an object-oriented language, and everything can be described by classes, and the same exception is a thing. There are a number of exception classes available in Java .
Multiple exceptions are stacked up, which is an anomaly system.
Throwable: is the parent class of the exception class, Super class. Exception exception, error.
The system of anomalies is as follows (diagram):
There are three main ways to learn thorwable:
1.toString (); Brief description of Thorwable: Class full Name: Package name + class name
2.getMessage (); thorwable String for more information
3.printStackTrace (); The exception is printed with the stack memory information
Packagestudy; Public classStar { Public Static voidMain (string[] args) {throwable T=NewThrowable ("This is an exception, please handle");//here is the exception object, so the exception//parameter can pass a getmessage come inSystem.out.println (T.tostring ());//java.lang.Throwable: This is an exception, please handleSystem.out.println (T.getmessage ());//This is an exception, please handleT.printstacktrace ();//Print an unusual stack of memory information }}
Error Error: Errors are generally problems caused by Java virtual machines or hardware, called errors. So we don't usually use code to handle errors.
such as: byte[] buf = new byte[1024*1024*1024];
Syntax is not error, Java Virtual machine can only manage 64M memory by default, so because of problems caused by Java or hardware we call it wrong
Exception Exception: All exceptions are inherited from Exception.
Differences between errors and exceptions:
such as: Java.lang.OutOfMemoryError This is a type of error (end with an error)
Aclnotfoundexception this is an exception ( ending with exception, except for the end of the error )
Handling of Exceptions:
mode one: Catch exception
The processing format for catching exceptions:
try{
Code that might have occurred an exception
}catch (the type variable name of the caught exception) {
Code to handle exceptions
}
1 Packagestudy;2 3 4 Public classStar {5 Public Static voidMain (string[] args) {6 7 int[] arr = {};8Arr=NULL;//java.lang.NullPointerException NULL pointer exception9Test (4, 0,arr);//java.lang.ArithmeticException:/by ZeroTen } One Public Static voidTestintAintBint[] arr) { A - - //Catching exception handling the intc = 0; - - Try { - //code that may appear to be abnormal + System.out.println (arr.length); -c =b; +}Catch(ArithmeticException E1) { ASYSTEM.OUT.PRINTLN ("Handling exceptions with a divisor of 0"); at}Catch(nullpointerexception E2) { -SYSTEM.OUT.PRINTLN ("Handling NULL pointer exceptions"); -}Catch(Exception e) {//The parent class is placed on the last side, and if the exception is handled at the top, the subsequent exception is not executed. This can handle other exceptions that are not in the above. -SYSTEM.OUT.PRINTLN ("All exceptions are handled here.")); - } - inSystem.out.print ("c =" +c); - } to}
Points to note for catching exception handling:
1. If there is a problem with the method in the try, the code outside of Try-catch will continue to be executed after processing.
The code inside the 2.catch will only be executed if the code in the try is abnormal.
3. In a try, you can use many catch blocks, which means that a try can handle a very good exception.
4.try catch exceptions are caught in order from small to large. The bottom is the largest exception of the parent class. The person will be given an error.
The effect of catching exceptions:
At present, we can not see that when learning Java EE often used (knowledge point later to fill up);
Why not use the maximum exception directly:
In order for us to pinpoint the source of the error with precision and speed. such as: null pointer exception, with the largest exception, it can not be seen to be null pointer exception.
Way two: Throw an exception
throws exception handling key two keywords (throw, throws)
Throw, throws use note points:
The 1.throw keyword is used in methods, and throws is used on method declarations.
The 2.throw keyword is used to throw an exception inside a method, and throws is used to throw an exception on a method declaration.
There can be only one exception after the 3.throw keyword, throws can declare multiple exceptions.
To throw an exception, use note points:
1. If you throw an exception object in the method, you must throw the exception on the method declaration.
2. If a method invokes a method that throws an exception, the caller must handle the exception that is thrown.
3. If an exception is run out of a method, the code behind the throw will no longer be executed.
4. In one case only one exception can be thrown. (See Code)
When to throw an exception, when not to throw it?
You need to notify the caller (who called your code) that your code may be having problems, and this time you need to throw this exception to let the caller resolve the process.
If the code is directly dealing with the user, this time encountered an exception, you need to deal with the code, the user does not know what this code means, in a different way to let users know what this is.
1 Packagestudy;2 3 4 Public classStar {5 Public Static voidMain (string[] args) {6 Try {7 int[] arr = {};8arr =NULL;9Test (4, 0, arr);Ten}Catch(ArithmeticException e) { OneSYSTEM.OUT.PRINTLN ("Handling Exception"); A } - - } the Public Static voidTestintAintBint[] arr)throwsarithmeticexception,nullpointerexception{//Be sure to declare this exception - //the previous use of Try-catch is used to handle exceptions, and then the following code continues to execute - //now it's time to throw out the exception code and give it to the caller (the upper level in the main function) - + if(b = = 0) { - Throw NewArithmeticException ();//there could be this wrong object, throw it out +}/*else if (arr = = null) {//If the code is executed above, the code behind the throw will no longer execute A throw new NullPointerException (); at } */ - -System.out.println ("La la la")); - intc =b; - //System.out.println (c);//If you throw it out, C doesn't make sense, and you don't have to print. - } in}
Custom exceptions
Requirements: Analog Feiq (Flying autumn) on-line, found that the network cable is not plugged in, this time to throw an exception (prompting the programmer cable is not plugged);
Premise: Customizing a class inherits from exception
Problem: Sometimes throwing exceptions must be declared, and sometimes throwing exceptions does not need to be declared.
Exception architecture: After Exception is also divided into runtime exceptions and compile-time exceptions;
Run is an exception: when you throw a run-time exception, you can not declare the exception on the method, and the caller can not handle it.
Compile-time exception: When a compile-time exception is thrown, the exception must be declared on the method method, and the caller must handle it.
How to differentiate between compile-time and run-time exceptions:
Those are run-time exceptions: RuntimeException the exception itself, or subclasses that inherit from the exception are run-time exceptions.
Other than runtime exceptions are compile-time exceptions. Exception is a compile-time exception.
1 classNoipexceptionextendsexception{2 3 //Customize the exception of a cable that is not plugged in4 Publicnoipexception (String message) {5 Super(message);//If you do not write the default call to the empty constructor method of the parent class6 }7 }8 9 Public classStar {Ten Public Static voidMain (string[] args)throwsnoipexception{ One A //with Feiq -String IP = "192.168.10.100"; - theTest (IP);//here is the error, two options, can continue to throw, can be disposed of. - Throw NewNoipexception ("Not inserted well, network cable please plug the cable");//again, this is the Java virtual machine. - } - Public Static voidTest (String IP)throwsnoipexception{ + if(IP = =NULL) { - Throw NewNoipexception ("Not inserted well, network cable please plug the cable");//here is thrown to the caller (main function) + } A } at}
Introduction to exceptions and errors in Java learning, catching exceptions, throwing exceptions, custom exceptions (small records in Java learning)