Home Tutorial Java Core The throw keyword example in java

 
 

The throw keyword example in java
Posted on: March 31, 2010 at 12:00 AM
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

Related Tags for The throw keyword example in java:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.