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 throws Keyword


throws statement

Throws keyword allows us to execute multiple exceptions from a method.

If a method is generating an exception bu cannot handle it, then it is the job of method to specify the task to protect them from an Exception.

The exception that can be generated by method must be thrown by using throws clause otherwise the compile time error will be generated.

Syntax

type methodName() throws ExceptionType1,ExceptionType2...ExceptionTypeN
{
    // body of the method
    ...
    ...
    
    throw new ExceptionType ("String"); // required for executing catch block.
    //or 
    throw ThrowableInstance
}

Above method can be written inside the other try block or any other method. Compiler calls to particular catch block from the throw statement.

Example

class ThrowsTest
{
   
    static void throwsMethod() throws ArithmeticException
    {
        int a = 10, b = 0 , c;
	    c = a/b;
	    throw new ArithmeticException ("Error");
    }		
	public static void main(String args[]) 
	{
		try
		{
		throwsMethod();
		}
		catch(ArithmeticException e)
		{
			System.out.println("Divided by zero error");
		}
	}
}

<< 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