Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Introduction of Errors
Java Exceptions
Java Try Block
Java Catch Block
Java Finally Block
Java Throw Keyword
Java Throws Keyword
Java Builtin Exceptions
User Defined Exceptions

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java throw Keyword


throw

Throw keyword is used for manually (explicitly) throwing an exception.

Since throw is used for manually throwing an exception it is used within the method.

It doesn't allows us to throw multiple exceptions at a time. We have to go for the throws keyword.

Syntax

throw ThrowableInstance

or 

throw new ExceptionType ("String");

Important points

  • Throwable instance must be an object of type Throwable or subclass of Throwable.
  • Primitive types such as int , char,String and object are non throwable classes cannot be used as Exceptions.
  • We can obtain a Throwable object using either parameter in catch clause or by creating new operator (Unknown object).
  • We should avoid further statements after throw keyword because code becomes unreachable from that throw statement.

Example

try
{
    throw new NullPointerException ("Null Exception");
}
catch(NullPointerException e)
{
    System.out.println("Null pointer exception caught");
}

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo