Java exception is coming.

Source: Internet
Author: User

An exception is an error that occurs while the Java program is running.

An abnormal inheritance system

Throwable
* Error
* Exception
* RuntimeException

Two ways to handle exception
* a:try...catch...finally
* Try Catch
* Try Catch finally
* Try Finally
* B:throws
* Try...catch the way to handle multiple exceptions, but the first exception is captured and no longer executes

A: Differences in compile-time exceptions and run-time exceptions
* Exceptions in Java are divided into two main classes: compile-time exceptions and runtime exceptions.
* All instances of the RuntimeException class and its subclasses are called run-time exceptions, other exceptions are compile-time exceptions

* Compile-time exception
* Java program must display processing, or the program will be error, cannot compile
* Run-time exception
* No need to display processing, also can be treated as a compile-time exception

/**
* * Several common methods of a:throwable
* A:getmessage ()
* Get exception information, return string.
* B:tostring ()
* Gets the exception class name and exception information, and returns a string.
* C:printstacktrace ()
* Gets the exception class name and exception information, and where the exception appears in the program. return value void.
* B: Case Demo
* Basic use of several common methods of throwable
*/

Processing of Throws methods

/**
* * A:throws way to handle exceptions
* When defining a function method, you need to expose the problem to the caller to handle it.
* Then the method is identified by throws.
* B: Case Demo
* Examples to illustrate compile-time exceptions and run-time exceptions are thrown
* Compile-time exception throws must be processed
* Run-time exception throws can be handled or not handled
* @throws Exception
*/

The finally feature is

/**
* * Features of a:finally
* The statement body that is finally controlled is bound to execute
* Special case: JVM exits before execution to finally (e.g. System.exit (0))
* The role of b:finally
* For freeing resources, you will see in IO Stream operations and database operations
* C: Case Demo
* The features and functions of the FINALLY keyword
The *return statement is equivalent to the last breath of the method, so before he will die, he will see if there is a finally to help him complete his wish, and if so, will finally execute
* After a thorough return
*/

Class finally_demo{

public static void Main (string[] args) {
try {
System.out.println (10/0);
} catch (Exception e) {
System.out.println ("Divisor is zero");
System.exit (0);//This statement exits the JVM virtual machine
Return
} finally {
SYSTEM.OUT.PRINTLN ("See if I do it");
}
}

}

Execution Result:

The divisor is zero.
See if I'm doing this?

Overview of Throw
* In the function method within a certain situation, the program can not continue to run, need to jump, with throw to throw the exception object.

* The difference between c:throws and throw
* A:throws
* Used after the method declaration, followed by the exception class name
* Can be separated by commas with multiple exception class names
* Indicates a thrown exception, handled by the caller of the method
* B:throw
* Used in the body of the method, followed by the name of the exception object
* Only one exception object name can be thrown
* Indicates thrown exception, handled by the statement in the method body

Custom exception Classes

1) First create a custom exception class, syntax format: Custom exception class name extends Exception.

2) throws the exception object through the keyword throw in the method.

3) If an exception is handled in the method that currently throws the exception, it can be captured and processed with the Try-catch statement, or, if not, by the keyword throws at the declaration of the method indicating the exception to be thrown to the method call.

4) Catch and handle exceptions in the call to the exception method.

Example:

Class Numeratoriszeroexception extends Exception
{
Public numeratoriszeroexception (String msg)
{
Super (MSG);
}
}

public class number
{
public int divition (int inum1,int iNum2) throws Numeratoriszeroexception
{
if (0 = = iNum2)
{
throw new Numeratoriszeroexception ("Numerator cannot be zero.") ");
}

return (INUM1/INUM2);
}

}

Run:

public class Test
{
public static void Main (string[] args)
{
Number num = new number ();
Try
{
System.out.println ("Shang:" + num.divition (12,0));
}
catch (Numeratoriszeroexception e)
{
System.out.println (E.getmessage ());
E.printstacktrace ();
}
catch (Exception e)
{
System.out.println (E.getmessage ());
E.printstacktrace ();
}
}
}

Operation Result:

A concubine cannot be 0.
Com.heima.exception.NumeratorIsZeroException: The concubine cannot be 0
At Com.heima.exception.Number2.divition (number2.java:6)
At Com.heima.exception.Test.main (test.java:9)

Java exception is coming.

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.