A catch-all "exception handler" - what's the end of an stack trace?

I've created an application that is beeing tested these days, and I thought it would be a good idea to implement a "catch-all" exception handler so that I could notify the user when an exception occur (so that he may stop, send me the log, and to prevent errors that occur as a result of the first one).
The way I've started implementing it is I redirect error out to a custom output stream:
public class ConsoleOutStream extends ByteArrayOutputStream {
    private JFrame owner;
    public ConsoleOutStream(JFrame owner) {
        this.owner = owner;
     * Writes <code>len</code> bytes from the specified byte array
     * starting at offset <code>off</code> to this byte array output stream.
     * @param   b     the data.
     * @param   off   the start offset in the data.
     * @param   len   the number of bytes to write.
    public synchronized void write(byte b[], int off, int len) {
        super.write(b, off, len);
        checkForExceptions();
     * Writes the specified byte to this byte array output stream.
     * @param   b   the byte to be written.
    public synchronized void write(int b) {
        super.write(b);
        checkForExceptions();
    private void checkForExceptions() {
        reset();
        if(this.toString().indexOf("xception") != -1) {
            System.out.println("Exception!!\n\n");
            System.out.println(this.toString());
}then i redirect System.err:
PrintStream out = new PrintStream(new ConsoleOutStream(this));
System.setErr(out);
The problem is that the checkForExceptions() method will be called, e.g. 3 times for each exception - and I just want to display an error message to the user once (of course).
Anyone done something similar?

I'm interested in catching all "unhandled errors"how about:
public static void main(String[] args){
    try{
    //whatever you would normally call from main
    }catch (Throwable e){
         System.out.println("There was an unhandled exception:");
         e.printStackTrace();
}

Similar Messages

  • Best Practice: JavaFX pattern for "Catching all Exceptions"

    Hi,
    what is on the current JavaFX Standard the best way to catch all Exceptions (centralized) within my JavaFX application...
    I read thread outside this Oracle Forum who recommend following:
    1. Thread.setDefaultUncaughtExceptionHandler(new MyExceptionHandler());
    --> catch all runtime exceptions
    2. http://stackoverflow.com/questions/12318861/javafx-2-catching-all-runtime-exceptions
    --> Implementing some source code who wrap the current GUI thread...
    3. I read something like:
    "JavaFX exception handling is almost identical to that in Java, apart from the fact that checked exceptions are handled in the same way as unchecked exceptions. This is good news for most Java programmers moving to JavaFX because you are no longer obliged to catch and handle exceptions."
    Sounds very good! But where/how can I do this ???
    Edited by: wschele on 19.02.2013 04:58
    Edited by: wschele on 19.02.2013 05:16

    No recommendation whats the best way to do it?
    Catching each Exception in different layers is boring ! :-(

  • Best way to catch all exceptions

    What is the best way to proceed if I would like to catch all exceptions in my Swing app?
    If there is an unexpected RuntimeException in my app, I would like to display an error message rather than having the program react silently. Is there a good way of making sure all uncaught exceptions are trapped and reported somewhere in the end? And are there any special inconveniences to such a strategy?

    A very similar question was asked recently. Various solutions were proposed in [this topic|http://forums.sun.com/thread.jspa?forumID=57&threadID=5416873] .

  • HT201341 what is X11 all about? what is the benefit of having X11?

    what is X11 all about and what is the benefit of having X11?

    Welcome to Apple Support Communities
    If you ask this, you will not probably need it. X11 is a version of the popular X Window System, used by most UNIX operating systems (including OS X). Since OS X Lion (I think), it is no longer included, and you only need it to run some specific applications.

  • My iphone 5 has nod sound at all except for with headphones the volume keys says ringer I tried to restart my device and I reset my settings but nothing seems to work it happend to me 2 days ago and yesterday the sound came back for 10 minutes and then go

    Hey guys I hope u can help me out, my new 3 months old 16g iPhone 5 has problem
    The sound don't work at all except for with headphones, the volume says ringer.
    I've tried restarting my device, reset my settings I even tried using a Q-tip in the headphone jack.
    Please help me out.
    And btw yesterday it worked for like 10 minutes and then got back to no sound.
    And this silent mode isn't on.

    it's a hardware issue
    https://www.google.dk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&ved=0CC4Q FjAA&url=https%3A%2F%2Fdiscussions.apple.com%2Fthread%2F1343532%3Fstart%3D315%26 tstart%3D0&ei=Wy1hUYiWDs3Y4QSzqIHAAw&usg=AFQjCNHLi5JBEWvjM6SHBge36Or-m1YMEw&sig2 =-By7BjriUmZPDpHDnY_ySw&bvm=bv.44770516,d.bGE
    more hits from the search
    https://www.google.dk/search?client=opera&q=iphone+stuck+in+headphone+mode&sourc eid=opera&ie=utf-8&oe=utf-8&channel=suggest

  • Hi all, I wonder what is the different between iphone4 AT&T, sprint and verizon?

    Hi all, I wonder what is the different between iphone4 AT&T, sprint and verizon? does verizon is CDMA? does CDMA have a slot sim card? what is the version for iphone 4s AT&T? 5.0.1 or 5.1?

    AT&T is a GSM carrier. Sprint and Verizon are CDMA. The CDMA iPhone 4 has no SIM slot. The CDMA iPhone 4S does but the phones still can't be used on GSM in the U.S. The software versions are the same across carriers at this time.

  • My mac pro suddenly stops working (2011), connection to the monitor stops, but de tiny light on computer still on and all sound stops. what's the problem?

    my mac pro suddenly stops working (2011), connection to the monitor stops, but de tiny light on computer still on and all sound stops. what's the problem?

    When the display turns off, it is not clear whether the computer has halted, or whether the display subsystem has failed.
    Those tests determine whether:
    a) Your Mac software had stopped working  OR
    b) Only the display had stopped working
    If the pressing the Caps lock light on the keyboard lights the Caps Lock light on the keyboard, some software is still running -- your Mac has not halted.
    If pressing Control-Eject on the keyboard, followed by the Return key shuts down your Mac, the software was still running.
    ¿Software was still running?
    --Yes, the problem is ONLY in the display subsystem
    --No, this is a computer-wide problem such as a kernel panic

  • Catch All Exception

    Hi,
    I'm quite familiar with Oracle SOA, OSB , CEP stuff. Just exploring BPM and here i have a question on error handling.
    I see we can catch all system or business exceptions using a event sub process. and handle it. But i see no means to see what the error message is?
    Lets say i would like to email admin with the error message, from where can i retrieve the error message from?
    Any pointers on this is much appreciated.
    Thanks,
    Prakash

    I think you can use Error Events (Component Palette, from the Catch Events section select Error Event) and then log it or email it .
    Ref- 19.5 Handling Exceptions in a Business Process
    http://docs.oracle.com/cd/E21764_01/doc.1111/e15176/errors_bpmpd.htm
    Thanks
    Rupesh

  • Trying to catch all exceptions...

    Hi, developers!
    I am trying to develop the best code, that can catch all the exceptions, in the best possible way, and whenever as possible it must register in a log with informations about the exception occurred.
    I need your suggestions. See the code below:
    MyClass()
      throws IOException, MyException {
      Throwable objThrowable1 = null;
      try {
        doSomething();
      } catch(MyException e) {
        objThrowable1 = e;
        throw e;
      } catch(IOException e) {
        objThrowable1 = e;
        throw e;
      } catch(RuntimeException e) {
        objThrowable1 = e;
        throw e;
      } catch(Exception e) {
        objThrowable1 = e;
        throw new Exception("Some Exception occurred.", e);
      } catch(Error e) {
        objThrowable1 = e;
        throw e;
      } finally {
        if (objThrowable1 != null) {
          Throwable objThrowable2 = null;
          try {
            log.fatal(objThrowable1);
            objThrowable1.printStackTrace();
          } catch(RuntimeException e) {
            objThrowable2 = e;
            throw e;
          } catch(Exception e) {
            objThrowable2 = e;
            throw new Exception("Some Exception occurred while logging.", e);
          } catch(Error e) {
            objThrowable2 = e;
            throw e;
          } finally {
            if (objThrowable2 != null) {
              objThrowable2.printStackTrace();
    }It is the constructor of MyClass, and it might throw IOException or MyException.
    Now some questions:
    1) Do you think I exaggerated and wrote a lot of code, more than sufficient?
    2) I wrote all this code because I think it�s a good idea throwing exceptions, especially RuntimeException and Error. The Virtual Machine must know how to handle the situation when some exception occurs. But I want to register a log of the exception, too, whenever as possible. In my opinion, the only way to advise the Virtual Machine that some exception occurred is throwing this exception. Do you agree? Do I really need to worry about it?
    Thanks in advance!

    Hi, developers!
    I am trying to develop the best code, that can catch
    all the exceptions, in the best possible way,Define "best possible way". I don't think what you're proposing is even close, by any measure.
    You should only catch exceptions that you intend to handle. If there's no way for your class to handle the exception, it should bubble it up to the class that will. Catching and rethrowing like that seems a total waste to me.
    I need your suggestions. See the code below:I'd suggest that this is an ugly mess. I would not go this way.
    I have no idea whatsoever about that finally block. That should be for cleanup. What are you doing there?
    MyClass()
    throws IOException, MyException {
    Throwable objThrowable1 = null;
    try {
    doSomething();
    } catch(MyException e) {
    objThrowable1 = e;
    throw e;
    } catch(IOException e) {
    objThrowable1 = e;
    throw e;
    } catch(RuntimeException e) {
    objThrowable1 = e;
    throw e;
    } catch(Exception e) {
    objThrowable1 = e;
    throw new Exception("Some Exception occurred.",
    d.", e);
    } catch(Error e) {
    objThrowable1 = e;
    throw e;
    } finally {
    if (objThrowable1 != null) {
    Throwable objThrowable2 = null;
    try {
    log.fatal(objThrowable1);
    objThrowable1.printStackTrace();
    } catch(RuntimeException e) {
    objThrowable2 = e;
    throw e;
    } catch(Exception e) {
    objThrowable2 = e;
    throw new Exception("Some Exception occurred
    occurred while logging.", e);
    } catch(Error e) {
    objThrowable2 = e;
    throw e;
    } finally {
    if (objThrowable2 != null) {
    objThrowable2.printStackTrace();
    }It is the constructor of MyClass, and it might
    throw IOException or MyException.
    Now some questions:
    1) Do you think I exaggerated and wrote a lot of
    code, more than sufficient?
    2) I wrote all this code because I think it�s a good
    idea throwing exceptions, especially
    RuntimeException and Error. The Virtual
    Machine must know how to handle the situation when
    some exception occurs. But I want to register a log
    of the exception, too, whenever as possible. In my
    opinion, the only way to advise the Virtual Machine
    that some exception occurred is throwing this
    exception. Do you agree? Do I really need to worry
    about it?
    Thanks in advance!Cath t

  • How to catch ALL Exception in ONE TIME

    I'm explain my issue:
    I'm making a program with Class, Swing, Thread ...
    Then all action I do on my graphical application, I use a new thread, well
    I want to capture in my Startup programs, all unknow exception and then, I display it with a JOptionPane for example
    In fact, I want to do something like Eclipse, when it crash, I capture the error
    Could you help me ? Tell me the best way to do that ?
    This is an exemple
    FILE: Startup.java
    class Startup{
    public static main (String args[]){
    try{
    new Main();
    }catch(Throwable e){
    //Message d'erreur fenetre
    FILE: Main.java
    class Main{
    Main(){
    init_action();
    void init_action(){
    mybutton.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e){
    Thread th=new Thread(){
    public void run(){
    int a = 1 / 0;
    th.start();
    Well, in this example I want to capture the Divide By 0, I use the Throwable Exeption, in order to be sure I catch all unknow exeption
    Then, a good job, is to put throws Throwable in all function
    but Thread, and ActionPerformed ... could not implement it
    How to put this exception and jump it to Startup Program ?

    I already do that, what can I do for improving capture ?
    That's impossible ... It will be a great idea to make a Redirection of Error to a Exception class in futur version
    For example, when an unknow error arrive, don't show it on console and crash ... but run a class redirector exception, and magic, it show you a beautiful error warning, and stop properly the programme ...
    I put an error class, and put try {] catch {} everywhere, and run my exception class,
    this class detect the error exception and run a properly beautiful and clear french message (I'm french :d)
    Well, If you have the BEST other idea, tell me, I read your message with a lot of regard
    see you soon
    bye

  • When I copy an email message and paste it into a document, the font turns into all symbols.  What's the problem here?

    When I copy an email message and paste it into a document, the font becomes all symbols. What is wrong?

    To use images from the media browser, you must generate previews of the images first. It sounds like the "automatically generate previews for every new project" is turned off under Aperture >Preferences>Previews. To generate a preview for a photo to use in the media browser, right click it and select "update preview". The quality of the preview photo is determined by the settings in the Previews tab found under the Aperture>Preferences menu.

  • What is the best way to stack DAQ aquired data in labview?

    I'm developing an application to work with an M-series daq card and labview 8.5 to output a signal and then record on 8 differential inputs for a short period of time (~10 ms). I need to stack my data, however, because the incoming signal will be very, very small, even after amplification. So basically i'm running a slightly modified version of the multifunction Synch AI-AO.vi (included with the install of daqmx). What is the best way for me to rerun this vi a set number of times and add new data directly to the old data (not cat-ing or anything, like |sample 1 of run 1| + |sample 1 of run 2| = stacked stample 1).
    A slightly modified version of the mutlifunction synch AI-AO.vi is attached.
    Attachments:
    des_v2_Multi-Function-Synch AI-AO.vi ‏143 KB

    Hi LSU,
    see attachment on how to "stack" several measurements. I simply add the waveforms and use a shift register to keep the last iterations value.
    Writing to files in each iteration is extremly CPU consuming - especially with express vis. Using for loops for just one iteration is "senseless". You could enable the conditional terminal of the for loop to realize your stop feature.
    For your message 4:
    Have you ever tried all the things you asked for? Sometimes it's easiest to just try&error
    And for the "n=n+x" question: It really helps to take the free online courses offered by NI!
    Message Edited by GerdW on 11-11-2009 06:27 PM
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome
    Attachments:
    des_v2_Multi-Function-Synch AI-AO.vi ‏128 KB

  • Report to check all GL account wise at the end of year

    hii friends,
    Can we see all GL accounts wise report with opening balance, transaction and closing balance at the end of year.
    In FS10 N we are able to see one account no and it details or else we can see the total of all the balaces but not the a.c wise.
    can anybody help me out with any standard report or T.code to check no of GL accounts with the details of opening balance, transaction and closing balance.
    the purpose is for reporting at the end of year.
    Thanks in advance.....

    Hi,
    For genertaing the report as per ur requirement, u can follow the following path:
    SAP Easy Acess->Accounting->Financial Accounting->General Ledger->Information System->General Ledger Reports->Account balances->General->G/L Account balances/Totals and balances/Structured Account balances.
    Remenber in the selection criteria dont mention any particular G/L a/c no. if u want report for a number of G/L accounts.In that case, u can mention ur co. code or a range of G/L accounts as a selection criteria and the report will be generated accordingly.
    Hope this clarifies to some extent.
    Please reward points if u find this to be useful.
    Thanks,
    Prithwiraj.

  • TS1717 All my music stops before the end of the tune; whether bought through Itunes library or if loaded from my own personal collection.  This has only happened when i changed computer.

    When my music ois playing whether on computer or on iphone, every tune quits before the end, losing as much as a minute sometimes.  i know the full song is there because i see the timer moving towards the end but jumps to the next song before it gets anywhere near the end.
    All my downloads onto iphone have now got the same problem.  How to resolve?
    My computer is not an apple, the operating system is windows 7.
    Mike Dewar

    Hard to tell and remember whether it's happening just to pre-itunes plus music.  However, it's happening to many songs ripped from my personal CD collection.  With 10,000+ songs transferred from my own CD's I'm not high on re-ripping the entire collection.
    This seems to have happened in the past 6-12 months and can't help but wonder if one of the upgrades to iTunes somehow corrupted some file or meta data that is causing this effect.
    Gary

  • Trying to download itunes store on windows 7 all day it freezes near the end can anyone help please

    hi got my new iphone 4 today - have been trying to download itunes on my computer all day it freezes at the last minute. Have turned off security and firewall its still not working
    help please

    Have you tried a different web browser? Also, try temporarily disabling your antivirus software.

Maybe you are looking for