# throw Java Keyword

The throw keyword is used to raise an exception.

## Examples

      import java.io.IOException;

      public class MyClass
      {
         public method readFile(String filename) throws IOException
         {
            <statements>
            if (error)
            {
               throw new IOException("error reading file");
            }
         }
      }

## Remarks

- The throw statement takes a java.lang.Throwable as an argument. The Throwable is propagated up the call stack until it is caught by an appropriate catch block.

- Any method that throws an exception that is not a RuntimeException must also declare the exceptions it throws using a throws modifier on the method declaration.

Related Topics

catch Java Keyword

finally Java Keyword

throws Java Keyword

try Java Keyword