AppDividend
Latest Code Tutorials

Java Throw Exception Tutorial With Example | Java throw keyword

0

Java Throw Exception Tutorial With Example | Java throw keyword is today’s topic. The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. The caller must catch the exceptions. If you do not handle the exception in the try catch block, compiling will fail. But almost every other method in the java library or even user-defined may throw an exception or two.

Java Throw Exception Tutorial With Example

First of all, we need to know about what is an Exception. An exception is a kind of error at the runtime. There are a lot of exceptions in Java like ArithmeticException, NullPointerException, ArrayIndexOutOfBound exception, etc.

Suppose we are dividing a number by zero, it will not return any compilation error, but it will return runtime error, and it’s under ArithmeticException. Again, if we add a further element in Array beyond its capacity, then it returns ArrayIndexOutOfBound exception.

We can define our own sets of rules or conditions and throw an exception explicitly using throw keyword. We can set a condition like dividing by 5 will return ArithmeticException, and we can throw it using throw keyword. We can also use throw keyword in Custom Exception.

#Syntax of throw keyword

throw Instance;

Now, see the following code.

throw new ArithmeticException (“divided by zero”);

Let’s take a simple example to understand throw exception better way.

// ThrowExceptionExample.java

class ThrowExceptionExample
{
  static void valid(int age)
  {
	  if(age<18)
	  {
		  throw new ArithmeticException("Not Age to vote.");
	  }
	  else
		  System.out.println("You are welcome to vote.");
  }
  public static void main(String []er )
  {
	  valid(13);//throws example
	  valid(18);//does not throw example 
	  
  }
}

See the following output.

T_notValid

 

The flow of execution of a program stops after the throw statement is executed. The nearest try block is checked to see if the program has a catch statement which matches the type of exception. If it finds a match, it transfers the control to the statement else next try block is checked. It’ll be checking if there is no match. Finally, if there no match found in the whole program then default halter will halt the program.

Let’s see an example regarding this to understand this better.

// ExceptionExample.java

import java.io.*;
import java.util.*;

class InvalidStringException extends Exception
{
	public String toString()
	{
		return "String is ODD";
	}
}
class MyString
{
	MyString(String s) throws InvalidStringException
	{
		int l=s.length();
		if(l%2==0)
		{
			System.out.println(s);
		}
		else
		{
			throw new InvalidStringException();
		}
	}
}
class ExceptionExample
{
	public static void main(String []er)
	{
		Scanner sc=new Scanner(System.in);
		System.out.println();
		//entering a string 
		String a=sc.next();
		try
		{
			MyString m=new MyString(a);
			
		}
		catch(InvalidStringException e)
		{
			System.out.println(e);
		}
	}
}

See the following output.

Java throw keyword

 

#What is throws keyword in Java

The throws keyword in JAVA is used to declare that the method may throw one or some exceptions. The caller must catch the exceptions.

If you do not handle the exception in a try catch block, compiling will fail. But almost every other method in the java library or even user-defined may throw an exception or two.

#What is the difference between throw and throws

throw
throws
It is used to create the new Exception object and throw it It is used in the method definition, to declare that the risky method is being called.
Using a throw keyword, you can declare only one Exception at a time Using throws keyword, you can declare multiple exceptions at the same time.
Example:

throw new IOException(“can not open connection”);

Example:

throws IOException, ArrayIndexBoundException;

Finally, Java Throw Exception Tutorial With Example | Java throw keyword article is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.