Java Exception Review

Source: Internet
Author: User
Tags terminates throw exception throwable

If there are too many concepts to learn, you can learn the opposite, from the results to the process, from the code to the concept, it may not be so dull, such as learning reflection.

Java Exception Review

What are the differences between exceptions and errors?

Exception: An error occurred in the program or environment itself. (programmers can capture and process)

Error: Internal system error. (Programmers don't have to do anything, the programmer notifies the user to turn it off.) )

Class scanner, nextint usages and their meanings:

ImportJava.util.Scanner; Public classtestexception2{ Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); //        intb; Try{System.out.println ("Input dividend:"); A=Input.nextint (); System.out.println ("Input divisor:"); b=Input.nextint (); System.out.println ("The result of dividing two numbers is:" +a/b); }Catch(inputmismatchexception e) {System.out.println ("You don't enter a number, it must be a number. "); }            }}

/*

Class Scanner: One can use regular expressions to parse basic types

and a simple text scanner for strings.

For example: The user can read a number from the system.in:

Scanner sc=new Scanner (system.in);

int I=sc.nextint ();

Nextint (): method in Class Java.util.Random, returns the next pseudo-random number (here the random is relative)

An int value that is uniformly distributed in a random number generator

Here is an example of reading several items from a string:

String input = "1 Fish 2 fish Red Fish Blue Fish";      New Scanner (Input). Usedelimiter ("\\s*fish\\s*");     System.out.println (S.nextint ());     System.out.println (S.nextint ());     System.out.println (S.next ());     System.out.println (S.next ());     

The output is:

1

2

Red

Blue

How do you make a skip read?

*/

Summary of Exception syntax format:

try{

Code that the program needs to execute may be wrong

}catch (Exception class variable name) {

If an exception class occurs, the code block to be executed

}

The exception class is defined or inherited by the exception class or subclass.

Exception object one is the runtime environment automatically thrown, such as the divisor can not be zero, and the second is user-defined.

finally: the code that must be executed in any case, finally the only thing that is not executed is the existence of system.exit () that terminates the operation of the JVM.

ImportJava.util.InputMismatchException;ImportJava.util.Scanner; Public classtestexception{ Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); intb; Try{System.out.println ("Input dividend:"); A=Input.nextint (); System.out.println ("Input divisor:"); b=Input.nextint (); System.out.println ("The result of dividing two numbers is:" +a/b); }    Catch(inputmismatchexception e) {System.out.println ("You're not typing a number, it has to be a number!"); }Catch(ArithmeticException e) {System.out.println ("The number you entered is 0");    Sytem.out.println (E.getmessage ()); }Catch(Exception e) {System.out.println ("The program went wrong!" ")    }finally{System.out.println ("Whether it's unusual or not, it will be done here!" "); }    }}

4.throws

Some abnormal users do not want to process, the user may not be able to deal with or do not want to process, you can add the method header throws statement to discard this method processing.

ImportJava.util.InputMismatchException;ImportJava.util.Scanner; Public classtestexception{ Public Static voidMain (string[] args)throwsinputmismatchexception, arithmeticexception,exception{Scanner input=NewScanner (System.int); intb; System.out.println ("Input dividend:"); A=Input.nextint (); System.out.println ("Input divisor:"); b=Input.nextint (); Sysem.out.println ("The result of dividing two numbers is:" +a/b); }}

If the JVM does not find an exception code block when it goes back to the bottom of the call stack, the Main method

1) Call the Exception object Printstacktrace () method to print the exception information for the method call stack.

2) If the exception thread is the main course, the program terminates, or the other thread continues without the main thread.

Description: Using throws is a kind of negative abnormal usage, so it is not advocated, and consumes more resources and time.

(Increased uptime of the JVM)

5. Custom exception: Class Java.lang.Throwable is the base class for all classes, and it includes two subclasses: Exception and error. Exception is

Describes the exceptions that the program catches, such as classnotfoundexception. The error class is used to indicate a reasonable application but because

JVM errors, such as virtual machine error virtualmachineerror. (the error does not allow the programmer to capture)

Custom exceptions can inherit the Throwable class or exception, and do not inherit the error class. And there can be inheritance between custom exceptions.

Public to design a work function for a custom exception, construct a custom exception object for convenience.

 Public class extends   public myexception () {  super public myexception ( String msg) {  super(msg);}}

The MyException class above is called an exception class, and the exception class throws the exception manually by throwing the keyword .

To manually throw exception syntax formats:

throw new Exception Class ();

Such as:

 Public voidSetage (intAgethrowsmyexception{if(age<0 | | age>100){     Throw Newmyexception ("Age does not meet the requirements"); }Else{           This. age=Age ; }} ...//in the test classTry{t1.setage (1000);}Catch(MyException e) {e.printstacktrace ();}

Or

In the test class, throws is used to inherit myexception, which is given to the JVM to handle

That is, public static void main (string[] args) throws myexception{...}

Java Exception Review

Related Article

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.