Java Handy Note Six exception

Source: Internet
Author: User
Tags throwable

Static Import: Import static package name. Class name. static member name; tells the compiler which class the static member used in the current class imports the static package name. *; Note: Static import simplifies code writing, but results in poor reading: Error exceptions that occur when the program runs: Exception class The program must accept exceptions that are not checked and handled: RuntimeException or Error Can not accept check and handle or fatal error exception throwable--error--exception--runtimeexceptionjava.lang.throwable class for all Exception parent class exception The subclass exception also matches the condition GetMessage () return error message Printstacktrace () position error reason name stack information handling exception: 1. Capture; try{} used to detect possible code catch (Exception object variable name) {} The code that handles the exception finally executes the statement, releasing the function of the resource note: Finally, the return will also execute when it encounters the system. Exit () The output must be displayed by the program if the exception is not performed. Try/[catch]*/finally2. Throw; throw: Used to throw an exception object inside a method, execute the statement, return---Throw a non-detected exception--Throw a detected exception must handle the exception is usually declared on the method, then call throws: Declare the exception, On a method, you can declare multiple exception custom exceptions: 1. Create a class that inherits exception or Runtimeexception2. Call the constructor (typically a constructor that calls a string type argument) throws declare the exception on the method to throw in the method Throwing an exception subclass overrides the parent class method: The child class declares the exception object's method to be less than the scope subclass of the parent exception object and can also not declare an exception. The parent class method does not declare that the exception subclass override method cannot declare exception 3. Capture and then throw the package com.orancle;

public class TestException {
public static void Main (string[] args) {
try{
int s = divide (5,1);
System.out.println ("s" +s);
s = Divide (5,0);
System.out.println ("s" +s);
}catch (Llleagalnumberexception message) {
System.out.println ("Catch exception:" +message);
}
System.out.println ("--end");
}

public static int divide (int m,int n) throws llleagalnumberexception{
int s;
try{
s = m/n;
}catch (ArithmeticException message) {
throw new Llleagalnumberexception ("Divisor cannot be zero");
}return (s);
}
}package com.orancle;

public class Llleagalnumberexception extends exception{
Public llleagalnumberexception (String message) {
Super (message);
}
}

The access permission modifier 1, private permissions can decorate a data member, construct a method, a method member, cannot decorate a class (here refers to an outer class, regardless of the inner class).   Members that are decorated by private can only be used in the class in which they are defined and cannot be called in other classes. 2, the default permission class, the data member, the constructor method, the method member, all can use the default permission, namely does not write any keyword.   The default permissions are the same as package permissions, and the elements of the same package permission are called only in the class in which they are defined, and in the same package class. 3. Protected permissions (Protected) protected can decorate data members, construct methods, method members, and cannot decorate classes (this refers to external classes, regardless of inner classes). Members that are modified by protected can be called in the class that defines them, in the same class as the package.   If classes with different packages want to invoke them, then the class must be a subclass of the class that defines them. 4. Public permissions can be decorated with classes, data members, constructor methods, method members. Members that are modified by public can be called in any class, regardless of the same package or different packages, which is the most privileged modifier. %d string type placeholder System.out.printf ("%d-year-old,%d-foot,%d-hand. ", Age,hand,leg); class access control modifier: public: All default: Same Package access G

Java Handy Note Six exception

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.