Home>

java throw:throw an exceptionA program can throw an explicit exception with a throw statement.The general form of a throw statement is as follows:

throw throwableinstance;

Here, the throwableinstance must be an object of the throwable class type or the throwable subclass type.Simple type,Int or char, and non-throwable classes, such as string or object, cannot be used as exceptions.There are two ways to obtain throwable objects:using parameters in the catch clause or creating them with the new operator.

Program execution stops immediately after the throw statement;Any subsequent statements are not executed.The tightest surrounding try block is used to check if it contains a catch statement that matches the exception type. If a matching block is found,Control passes to the statement;If not found,Enclosing try blocks to check,And so on.If no matching catch block is found, the default exception handler interrupts program execution and prints the stack trace.

Here is an example program that creates and throws an exception,The handler that matches the exception then throws it to the outer handler.

//demonstrate throw.
class throwdemo {
 static void demoproc () {
  try {
   throw new nullpointerexception ("demo");
  } catch (nullpointerexception e) {
   system.out.println ("caught inside demoproc.");
   throw e;//rethrow the exception
  }
 }
 public static void main (string args []) {
  try {
   demoproc ();
  } 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 sets up another exception handling relationship and immediately throws a new nullpointerexception instance, which is captured on the next line.The exception was then thrown again.Here is the output:

caught inside demoproc.
recaught:java.lang.nullpointerexception:demo

The program also explains how to create Java's standard exception objects,Pay particular attention to the following line:

throw new nullpointerexception ("demo");

Here, new is used to construct a nullpointerexception instance. All java built-in runtime exceptions have two constructors:one has no parameters,One takes a string parameter.When the second form is used,The parameter specifies a string describing the exception.If an object is used as a parameter to print () or println (),The string is displayed.This can also be achieved by calling getmessage (), which is defined by throwable.

java throws clause

If a method can cause an exception but not handle it,It must specify this behavior so that callers of methods can protect themselves against exceptions.To do this you can include a throws clause in the method declaration. A throws clause lists all the types of exceptions that a method may throw.This is necessary for all exceptions of type except error or runtimeexception and their subclasses.All other types of exceptions that a method can throw must be declared in the throws clause.If you don't,Will cause compilation errors.

Here is the general form of a method declaration with a throws clause:

type method-name (parameter-list) throws exception-list {
 //body of method
}

Here, exception-list is a comma-separated list of exceptions that this method can throw.

Here is an incorrect example.This example attempts to throw an exception that it cannot catch.Because the program does not specify a throws clause to declare this fact,The program will not compile.

//this program contains an error and will not compile.
class throwsdemo {
 static void throwone () {
  system.out.println ("inside throwone.");
  throw new illegalaccessexception ("demo");
 }
 public static void main (string args []) {
  throwone ();
 }
}

To compile the program,Two things need to change.First, you need to declare that throwone () raises illegalaccess exception. Second, main () must define a try/catch statement to catch the exception.The correct example is as follows:

//this is now correct.
class throwsdemo {
 static void throwone () throws illegalaccessexception {
  system.out.println ("inside throwone.");
  throw new illegalaccessexception ("demo");
 }
 public static void main (string args []) {
  try {
   throwone ();
  } catch (illegalaccessexception e) {
   system.out.println ("caught" + e);
  }
 }
}

Here is the output of the example problem:

inside throwone
caught java.lang.illegalaccessexception:demo
  • Previous JavaScript determines the type of file uploaded by the FileUpload control
  • Next Record the deadlock process of the company's warehouse database server and its solution