Exceptions in Java

Source: Internet
Author: User

Exception Exception in Java

The Java.lang.Exception class is the direct or indirect parent class for all exceptions in Java. That is, the exception class is the root class for all exceptions.

Cases:

public class Exceptiontest
{
public static void Main (string[] args)
{
int a = 3;
int b = 0;
int c = A/b;
System.out.println (c);
}
}

Compile-through, execution-time results:

Exception in thread "main" java.lang.ArithmeticException:/By zero

At Com.learnjava.exception.ExceptionTest.main (exceptiontest.java:9)

Because the divisor is 0, an arithmetic exception is thrown.



More common exceptions are: null pointer exception, array subscript out of bounds exception

Java.lang.NullPointerException is a null pointer exception
Java.lang.IndexOutOfBoundsException is subscript out-of-bounds exception

Exceptions in Java fall into two main categories:

1.Checked Exception (non-runtime Exception, compile-time exception)

2.Unchecked Exception (runtime Exception, runtime exception)

The RuntimeException class is a subclass of the exception class and is a run-time exception. All runtime exceptions in Java inherit the RuntimeException class, either directly or indirectly.

Exceptions in Java that inherit exception but do not inherit the RuntimeException class are non-runtime exceptions.

General structure of exception handling

Try
{
Code that might have occurred an exception
If an exception occurs, the code after the exception is not executed
}
catch (Exception e)
{
Exception handling code
}
Finally
{
The finally statement block is executed whether or not an exception occurs
}

For example, the first Division operation code, after adding exception handling:

public class Exceptiontest
{
public static void Main (string[] args)
{
int c = 0;
Try
{
int a = 3;
int b = 0;

c = a/b;//This code has an exception, then the code after the exception is not executed

System.out.println ("No exception occurred");
}
catch (ArithmeticException e)
{
E.printstacktrace ();
}
Finally
{
The finally statement block is executed whether or not an exception occurs
SYSTEM.OUT.PRINTLN ("finally Statement! ");
}

System.out.println (c);
When B is 0 o'clock, there is an exception, the output is the initial value of C 0
}
}


Multiple catch

A try can be followed by multiple catch, but no matter how many, only one catch block will be executed at most.
When we use multiple catch blocks to catch an exception, we need to put the catch block of the parent type after the catch block of the child type, so that the subsequent catch block may be executed, otherwise the child type's catch block will never arrive, and the Java compiler will error.

If the exception types are independent, then their order is not required.


Exception Handling methods

For non-runtime exceptions (checked exception), it must be processed, otherwise it cannot be compiled.

There are two ways of handling this:

1. Use try: Catch.. finally to capture;

2. After the method declaration that generates the exception, write the throws a Exception type, such as throws Exception, and throw the exception to the outside layer.

For try: Catch.. Finally
Try and catch can be used together, try-catch-finally can use, but try,catch,finally is not available alone, if you want to use only try in the program and do not want to use catch can also, but the try must have a finally after.

Custom exceptions

The so-called custom exception is usually defined as a class that inherits the exception class or its subclasses. Because the exception must inherit the exception class directly or indirectly.

Typically, the exception class is inherited directly, and typically does not inherit from a runtime exception class.

Examples of custom exceptions:


public class MyException extends Exception
{
Public MyException ()
{
Super ();
}
Public myexception (Exception e) {
Super (E);
}
Public myexception (String message)
{
Super (message);
}
}




Note: When there is a return statement in a try block, the statement in the finally block is still executed first, and then the method returns.

If System.exit (0) is present in the try block, then the code in the finally block is not executed because System.exit (0) terminates the currently running Java Virtual machine, and the program ends execution before the virtual machine terminates.

Exceptions in Java

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.