Example: Normally built-in exceptions are thrown by the JVM but user can also create and throw any exception although this is normally not done. The throw statement is needed for throwing user-defined exceptions, which is discussed in a later section.
The following program creates and throws an exception
class ThrowDemo {
static void demoproc() {
try {
int c = 0, a = 0;
if (a == 0)
int b = c / a;
} catch (ArithmeticException e) {
System.out.println("Caught inside demporoc.");
throw e;
}
throw new ArithmeticException("Division by zero");
}
public static void main(String args[]) {
try {
demoproc();
} catch (ArithmeticException e) {
System.out.println("Recaught: " + e);
}
}
}
Output:
Caught inside demporoc.
Recaught: java.lang.ArithmeticException: Division by zero
Example: The following example demonstrates that if a checked-exception is thrown using throw statement then it must either be caught or declared in the throws clause.
import java.io.*;
class ThrowsDemo {
static void throwOne() throws IOException {
System.out.println("inside throwOne");
throw new IOException("Demo");
}
public static void main(String args[]) {
}
}
Output: above program will not compile and result in following compile time error.
throwOne();
Example: The above example is re-written such that the method throwOne() is called in a try block.
import java.io.*;
class ThrowsDemo
{ static void throwOne() throws IOException
{ System.out.println("inside throwOne");
}
public static void main(String args[])
{ try
throw new IOException("Demo");
{ throwOne();
}
catch(IOException e)
{ e.printStackTrace();
}
}
}
Output: results in an exception at run-time.
The following program creates and throws an exception
class ThrowDemo {
static void demoproc() {
try {
int c = 0, a = 0;
if (a == 0)
int b = c / a;
} catch (ArithmeticException e) {
System.out.println("Caught inside demporoc.");
throw e;
}
throw new ArithmeticException("Division by zero");
}
public static void main(String args[]) {
try {
demoproc();
} catch (ArithmeticException e) {
System.out.println("Recaught: " + e);
}
}
}
Output:
Caught inside demporoc.
Recaught: java.lang.ArithmeticException: Division by zero
Example: The following example demonstrates that if a checked-exception is thrown using throw statement then it must either be caught or declared in the throws clause.
import java.io.*;
class ThrowsDemo {
static void throwOne() throws IOException {
System.out.println("inside throwOne");
throw new IOException("Demo");
}
public static void main(String args[]) {
}
}
Output: above program will not compile and result in following compile time error.
throwOne();
Example: The above example is re-written such that the method throwOne() is called in a try block.
import java.io.*;
class ThrowsDemo
{ static void throwOne() throws IOException
{ System.out.println("inside throwOne");
}
public static void main(String args[])
{ try
throw new IOException("Demo");
{ throwOne();
}
catch(IOException e)
{ e.printStackTrace();
}
}
}
Output: results in an exception at run-time.