Syntax

Java Throw an Exception Syntax

In Java, throw an exception: throws an exception on purpose — throw new IllegalArgumentException("bad");

Quick Answer

Syntax
throw new IllegalArgumentException("bad");
JavaSyntax

Throw an Exception

Code example
throw new IllegalArgumentException("bad");

Throws an exception on purpose.

Learn More

How to throw an exception in Java

  1. Open your Java file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Java Throw an Exception Syntax

Use throw new IllegalArgumentException("bad");.

Related Syntax References

Frequently asked questions

What is the Java syntax for throw an exception?

Use this pattern: throw new IllegalArgumentException("bad");

Can I copy this Java code example?

Yes. Use Copy code and adapt the example to your program.

Is this Java syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify