top of page
Search
Writer's pictureAbhinaw Tripathi

throws Clause Example Java


throws Clause

Even if the programmer is not handling run-time exceptions,the java compiler will not give any error related to run-time exception.But rule is that programmer should handle checked exceptions.In case the programmer doe not want to handle the checked exception ,he should throw them out using throws clause.if it is not handled,the compiler expects at least to throw it out.

Program:

/**

*

*/

package com.collectionpack;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

/**

* @author Abhinaw.Tripathi

*

*/

class Sample

{

private String name;

public void accept() throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter name:");

name=br.readLine();

}

public void display()

{

System.out.println("Name:"+name);

}

}

public class ThrowsClauseApp

{

/**

* @param args

* @throws IOException

*/

public static void main(String[] args) throws IOException

{

Sample s=new Sample();

s.accept();

s.display();

}

}

Result:

The Java compiler expects here to handle the IOException using try-catch block but as i did not want too then i need to throws like this way.

throw Clause:

There is also a throw clause available in Java to throw an exception explicitly and catch it.

Program:

/**

*

*/

package com.collectionpack;

/**

* @author Abhinaw.Tripathi

*

*/

class ThrowClass

{

public static void demo()

{

System.out.println("Inside demo");

throw new NullPointerException("Exception Data");

}

}

public class ThrowClauseApp {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ThrowClass.demo();

}

}

Result:

Inside demo

Exception in thread "main" java.lang.NullPointerException: Exception Data

at com.collectionpack.ThrowClass.demo(ThrowClauseApp.java:15)

at com.collectionpack.ThrowClauseApp.main(ThrowClauseApp.java:27)

Note:

  1. throw clause is used in software testing to test whether a program is handling all the exceptions as claimed by the programmer.

  2. throw clause can be used to throw our own exceptions also.We can also create our own exceptions which are called User-Defined exceptions.We need the throw clause to throw the user-defined exceptions.

Types of Exception

As you have read out the exceptions,Lets move to understand the types of exception

  • Built-in Exceptions

  • User-defined Exceptions

Built-in Exceptions

  • ArithmeticException

  • ArrayIndexOutOfBoundsException

  • ClassNotFoundException

  • FileNotFoundException

  • IOException

  • InterruptedException

  • NoSuchFieldException

  • NoSuchMethodException

  • NullPointerException

  • NumberFormateException

  • RuntimeException

  • StringIndexOutOfBoundsException

User-defined Exceptions

User can also create his own exception which are called User-defined Exception.

Should use following steps to create:

  • The user should create an exception class as a subclass to Exception class. Since all exception are sub classes of Exception class,the user should also make his class a subclass to it.

class MyException extends Exception

  • User can write a default constructor in his own exception class.

  • User can create a parameterized constructor with a string as a parameter.He can use this to store exception details.He can call super class(Exception) constructor from this and send the string there.

MyException(String str)

{

super(str);

}

  • When the user wants to raise his own exception he should create an object to his exception class and throw it using throw clause as,

MyException me=new MyException("Exception Details");

throw me;

Program:

/**

*

*/

package com.collectionpack;

/**

* @author Abhinaw.Tripathi

*

*/

class MyException extends Exception

{

private static int accno[]={1001,1002,1003,1004,1005};

private static String name[]={"Abhinaw","Sandeep","Anand","Manish","pawan"};

private static double bal[]={10000,25000,58552,785585,8745895};

public MyException() {

// TODO Auto-generated constructor stub

}

public MyException(String str)

{

super(str);

}

}

public class UserDefinedExceptionApp

{

/**

* @param args

*/

public static void main(String[] args)

{

System.out.println("Accoun" + "\t" + "Name" + "\t" + "bal" + "\t");

for(int i=0;i<5;i++)

{

if(bal[i] < 10000)

{

MyException me=new MyException("The amount is less");

try {

throw me;

} catch (MyException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

}

3 views0 comments

Recent Posts

See All
bottom of page