moomoohk
moomoohk

Reputation: 493

Is there a way for me to throw an exception without it printing the stack trace?

The goal is to be able to do:

throw new RuntimeException();

without it printing:

Exception in thread "main" java.util.RuntimeException
at grame.GrameManager.add(GrameManager.java:40)
at grame.GrameManager.add(GrameManager.java:47)
at grame.Entity.<init>(Entity.java:56)
at grame.Entity.<init>(Entity.java:28)
at test.Test.main(Test.java:20)

(for example).

Is this possible? If so, how would I go about doing this?

Upvotes: 1

Views: 16063

Answers (8)

John Kane
John Kane

Reputation: 4443

All that you really need to do is catch it... However, this is a really bad idea. You may want to make your own exception and catch that. This way you will not swallow exceptions that you should not be ignoring. The only time that you should really consider to do this, is if you cannot allow your application to blow up. If that is the case then you should at the very least log the error.

This explains it better than I can (and is a good resource regardless). Basically, it suggests that:

"If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception".

Upvotes: 1

Matt
Matt

Reputation: 11805

No thrown exception ever directly generates a stacktrace to the error console. It's up to the code who is calling it to do so. In the case of a main program:

public static void main(String args[]) throws Exception {
   // do something that throws an exception
}

If you don't catch the exception, the system will actually spit it out to the console i believe.

Somewhere along the way, you need to deal with the exception. If showing it in the GUI is what you want, then you'll have to do something like this:

public interface ExceptionHandler {
     void handleException(Exception e);
}


public static void main(String args[]) {
  ExceptionHandler exceptionHandler = ...;

  try {
     // something that might throw an exception
  }
  catch (Exception e) {
     exceptionHandler.handle(e);
  }
}

Upvotes: 3

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

1. Java compiler only cares during compilation that you have given a catch for a try, whether you implement any code in the catch or not.

2. You can keep the catch block empty, or print it on the console, log it..etc....

eg:

try{

    }catch(Exception ex){



  }

3. But printStackTrace() prints the method name, class name , file name and the line number where the exception has occurred.

Upvotes: 0

fabiorocha
fabiorocha

Reputation: 150

Since exceptions are important, you could use a logging mechanism like log4j (http://logging.apache.org/log4j/1.2/) and set the logging to a different level when you don't want some exceptions to be printed or log to a file instead of console for example.

If you just don't care about the exception, catch it and do nothing with it (empty catch, which is awful).

Upvotes: 4

jrad
jrad

Reputation: 3190

Like this:

try {
    some code...
} catch (RuntimeException e) {

  }

Upvotes: 0

Adel Boutros
Adel Boutros

Reputation: 10285

You can redirect System.err by setting System.setErr(null);

Upvotes: 3

algorowara
algorowara

Reputation: 1720

If you want the method to kill the program without printing a stack trace, place it in a try/catch block, and under catch simply use the statement:

System.exit(1)

This lets the system know that the program exited with an irregular (non-zero) state, but does not print anything to System.err.

Upvotes: 1

Rob Wagner
Rob Wagner

Reputation: 4421

Just catch the exception, and don't put anything in the catch block.

I should add that doing this is generally a bad idea. Having that stack trace, or some sort of message is very useful when finding out what went wrong in your program.

Upvotes: 2

Related Questions