hhh hhh - 1 year ago 94
Java Question

Java: custom-exception-error

$ javac TestExceptions.java
TestExceptions.java:11: cannot find symbol
symbol : class test
location: class TestExceptions
throw new TestExceptions.test("If you see me, exceptions work!");
1 error


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

public class TestExceptions {
static void test(String message) throws java.lang.Error{

public static void main(String[] args){
try {
// Why does it not access TestExceptions.test-method in the class?
throw new TestExceptions.test("If you see me, exceptions work!");
}catch(java.lang.Error a){
System.out.println("Working Status: " + a.getMessage() );

Answer Source

Working Code

Try this:

public class TestExceptions extends Exception {
    public TestExceptions( String s ) {

    public static void main(String[] args) throws TestExceptions{
        try {
            throw new TestExceptions("If you see me, exceptions work!");
        catch( Exception a ) {
            System.out.println("Working Status: " + a.getMessage() );


There are a number of issues with the code you posted, including:

  • Catching Error instead of Exception
  • Using a static method to construct the exception
  • Not extending Exception for your exception
  • Not calling the superclass constructor of Exception with the message

The posted code resolves those issues and displays what you were expecting.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download