Java know how much (a) throw: Exception throws

Source: Internet
Author: User
Tags stack trace throwable

So far, you've just acquired exceptions thrown by the Java runtime System. However, a program can throw a definite exception with a throw statement. The usual form of a throw statement is as follows:
Throw throwableinstance;
Here, throwableinstance must be an object of the Throwable class type or Throwable subclass type. Simple types, such as int or char, and non-throwable classes, such as String or object, cannot be used as exceptions. There are two ways to get a Throwable object: Use a parameter in a catch clause or create it with the new operator.

The program execution stops immediately after the throw statement, and any subsequent statements are not executed. The most tightly enclosed try block is used to check if it contains a catch statement that matches the type of the exception. If a matching block is found, the control turns to the statement, and if it is not found, the second enclosing try block is checked, and so on. If no matching catch block is found, the default exception handler interrupts the execution of the program and prints the stack trace.

Here is an example program that creates and throws an exception, and the handler that matches the exception throws it to the outer handler.
1 //demonstrate throw.2 classThrowdemo {3     Static voidDemoproc () {4       Try {5          Throw NewNullPointerException ("Demo");6}Catch(NullPointerException e) {7System.out.println ("Caught inside Demoproc.");8          ThrowE//Rethrow the exception9       }Ten    } One  A     Public Static voidMain (String args[]) { -       Try { - Demoproc (); the}Catch(NullPointerException e) { -System.out.println ("Recaught:" +e); -       } -    } +}

The program has two opportunities to handle the same error. First, main () sets up an exception relationship and then calls Demoproc (). The Demoproc () method then establishes another exception-handling relationship and immediately throws a new NullPointerException instance, NullPointerException is captured on the next line. The exception is then thrown again. The following is the output result:

1 caught inside Demoproc. 2 Recaught:java.lang.NullPointerException:demo

The program also explains how to create a standard exception object for Java, paying particular attention to the following line:

1    Throw New NullPointerException ("demo");

Here, new is used to construct a nullpointerexception instance. All Java built-in runtime exceptions have two constructors: one with no parameters and one with a string argument. When the second form is used, the parameter specifies the string that describes the exception. If the object is used as a parameter to print () or println (), the string is displayed. This can also be done by calling GetMessage (), which getMessage () is defined by Throwable.

Series Articles:

Java know how much (top)

Java know how much (interface) interface

Java knows how much (40) the difference between an interface and an abstract class

Java know how much (41) generic explanation

Java know how much (42) the range of generic wildcard characters and type parameters

Java know how much (43) Exception Handling Basics

Java know how much (44) exception type

Java know how much (45) uncaught exceptions

How much Java knows (the) use of try and catch

Java know how much (47) use of multiple catch statements

Java knows how much (in) the nesting of Try statements

Java know how much (a) throw: Exception throws

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.