The throw keyword example in java


 

The throw keyword example in java

This tutorial will demonstrate the use of the throw keyword. In this example you will see a sub-class is made of Exception. The throw keyword helps to call the subclass of Exception class that you made.

This tutorial will demonstrate the use of the throw keyword. In this example you will see a sub-class is made of Exception. The throw keyword helps to call the subclass of Exception class that you made.

Code:

class MyMadeException extends Exception{}

class ExceptionExample
{
public static void main(String[] args)
{
String test = args[0];
try
{
System.out.println ("First");
doriskyJob(test);
System.out.println ("Forth");

} catch (MyMadeException e){
System.out.println ("Nothing");
}finally{
System.out.println ("Fifth");
}
System.out.println ("Sixth");
}
static void doriskyJob (String t) throws MyMadeException{
System.out.println ("Second");
if ("yes".equals(t)){
throw new MyMadeException();
}
System.out.println ("Third");
}
}

Hot to run program:

c:\yourjavadir> javac ExceptionExample.java

c:\yourjavadir> javac ExceptionExample Second

Output:

First
Second
Third
Forth
Fifth
Sixth

Ads