Java Basics-Exceptions

Source: Internet
Author: User

Whether you are a big God or a rookie like me, you will encounter such and such errors when knocking on the code, and the difference between us and the great God is that the great God encounters
Mistakes can be based on their own experience and error information is accurate to find "accident" location and solve it, and we often have to "fate" or panic God. To find the exact "accident."
Location plays an important role in improving the efficiency and the stable mentality. There's a way to find out what's wrong with the code, if you want to know what the "passion Red" is for your console.
The following are some of my "stock", if there is a mistake to welcome correct.

First, Relationship:

An exception (Exception) is a subclass under Throwable, and the Throwable class is the root class for all errors or exceptions in the Java language, including the exception Class (Exception) and
Wrong class (Error). Error generally occurs when the computer does not solve problems such as memory overflow, disconnection from the server, and so on. And the exception is what happens when you write it.
Errors or programs that may occur. Here the main research exception;

Second, the exception:

Exceptions can be subdivided into run exceptions (runtimeexception) and compile exceptions (Checked Exceptions) except for the exception of running exceptions, the rest of the exception are
Compile an exception, as the name implies, the exception is that you compile without error message only when you run it will give you the wrong information, and compile the exception is when you write code during the
will give you an exception to the error message.

Third, the solution:

The console may output an exception prompt when you are finished writing the code, and these exceptions are provided by the Java developer for us, and if you are unfamiliar with the ability to find the JDK or
Ask Google degree Niang. The exception for the console output is made up of 3 parts:
Example:Exception in thread "main" java.lang.arrayindexoutofboundsexception:11 at Com.jj.Demo02.main (demo02.java:6)
(1), exception type:Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: here array index out of bounds exception
(2), exception error hint: The index location of the cross-border
(3), abnormal position: atcom.jj.Demo02.main (demo02.java:6) package name. Class Name. Method Name

And sometimes it's not the code you write, considering the whole system's nature, some sensitive operations and possible error systems also require you to do exception handling, which is
There are 2 ways to solve this situation

1. Throw an exception:
Throws exception class name, exception class name 2 ...
Throw is thrown to the caller to resolve, who is called to resolve, and if no one resolves it then it is resolved by the Java Virtual machine
2. Catch exception:

1 Try {2// code 3 } with possible exception Catch (Exception class name variable name) {4// handle Exception code 5 } finally {6// must execute code that can be used for IO stream shutdown 7 }

Try is to resolve the exception yourself, and in addition to exception handling can sometimes be judged such as:

1Scanner sc=NewScanner (system.in);2arraylist<student> lists =NewArraylist<>();3 inti;4 //Input 3 times student Information5  for(i=0; i < 3; i++) {6 Try {7SYSTEM.OUT.PRINTLN ("Please input student information: (Input format: Zhang San, male, 25)");8 //Keyboard Entry9String name =sc.nextline ();Tenstring[] split = Name.split (","); OneInteger age = integer.valueof (split[2]); ALists.add (NewStudent (split[0],split[1],age)); -}Catch(Exception e) { - //when the input format is incorrect, the number of cycles minus one re-entry theI=i-1; -SYSTEM.OUT.PRINTLN ("The format you entered is not correct"); - } -}

Iv. How to define your own exceptions:

Declaring exceptions:
Throws exception class name, exception class name 2 ...
Example:

1 //The example here is a run-time exception, so the inheritance is RuntimeException2  Public classMyExceptionextendsruntimeexception{3 //overriding the construction method to define your own exception error prompt4  Publicmyexception () {5 Super();6 //TODO auto-generated Constructor stub7 }8 9  Publicmyexception (String message) {Ten Super(message); One //TODO auto-generated Constructor stub A } -  - } the //Main Method -  Public Static voidMain (string[] args) { -Name (110);  - } +  Public Static voidNameinta) { - if(a>100| | A<0) { + Throw NewMyException ("The score was wrong");  A } at}

Run Result:Exception in thread "main" com.jj.MyException: wrong result
At com.jj.test03.name (test03.java:10)
At Com.jj.test03.main (test03.java:5)

PS: No one seems to be so playful, my knowledge is less than I know.

Java Basics-Exceptions

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.