Results 1 to 5 of 5

Thread: [java] catch the exceptions in a proper way

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default [java] catch the exceptions in a proper way

    Hello,
    I'm new to Java and I found out that I sometimes do mistake missing try/catch blocks. I'm working on Eclipse. Just now I've finished a program but I'm wondering if I put all try/catch. Is there a way to check it? (tricks, maybe with ecplipse?).
    What do you suggest in general or in this case?

    thanks,,,
    Regards

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [java] catch the exceptions in a proper way

    as there no "the right place" to catch an exception, no.
    (It might be appropriate to wrap the statement that might throw itself, or a larger block, or the calling function call, even just to catch all the exceptions in "main". You have to decide where you want to deal with them. One argument for a catch block is: if want to retry an alternative to the failed code, or if you need to do cleanup...)

    HTH

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: [java] catch the exceptions in a proper way

    maybe my question was a bit different. Example: I wrote a program that it seemed work but I didn't catch an exception; I didn't see it. However, my program was working and i was lucky that in many "run" no problem related that bug appeared.
    How can I avoid this bad situation, I I worte another program and i'm scarred to have made the same mistake. do you understand now? thanks,
    Regards

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [java] catch the exceptions in a proper way

    I usually wrap my main eventloop with a try/catch-block. If an event (or external command) caused an exception I log (and ignore it, unless it is a known 'fatal' exception).

  5. #5
    Join Date
    Nov 2008
    Posts
    142
    Thanks
    3
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [java] catch the exceptions in a proper way

    Quote Originally Posted by mickey View Post
    I wrote a program that it seemed work but I didn't catch an exception; I didn't see it.
    Was it a NullPointerException? In general, you can't "miss" an exception, you have to handle it one way or another (catch it or pass it on to a higer level), otherwise the compiler will complain. There are, however, unchecked exceptions (those derived from RuntimeException, for which Java does not enforce these rules. I guess the one you missed was of this kind. To catch and handle those, study the documentation of the classes you use. The exceptions that could be thrown should be mentioned there. But in the end, you still have to decide where and how to handle an exception.

Similar Threads

  1. Exceptions and qApp->processEvents()
    By mcostalba in forum Qt Programming
    Replies: 3
    Last Post: 8th January 2006, 17:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.