Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Exception Handling

  1. #1

    Default Exception Handling

    Hi all, I am new to this forum so please be patient with me. I am a new programmer and I seem to be confused with the concept of exception handling. If we are to write code that would try a statement then catch the error, why wouldn't we be able to just anticipate that an error would occur and re-write the code so there wouldn't be a runtime/compile error, therefore eliminating the need of the try/catch statements? Am I missing something here? Thanks!

  2. #2
    Join Date
    Sep 2013
    Thanked 0 Times in 0 Posts

    Default Re: Exception Handling

    Well your are confusing compile time errors with run time errors. For example, if you were to accidentally forget to add a bracket for your construct:

    public class test {
    public test() {
    That is a compile time error, something the compiler can realistically predict. Where-as run time errors are things that having a compiler predict would either be far too expensive (performance wise), or just down right impossible (at this time).

    public class test {

    public static void main(String[] args) {

    try(BufferedReader br = new BufferedReader(new FileReader("file\\test.txt"))) {

    catch(IOException ioe) { System.out.println("you see Linux power you had that file on your computer like you were supposed to, however your clients were either blocking access or didn't have that file");}


  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    My Mood
    Thanked 263 Times in 232 Posts

    Default Re: Exception Handling

    Quote Originally Posted by LinuxPower View Post
    why wouldn't we be able to just anticipate that an error would occur and re-write the code so there wouldn't be a runtime/compile error, therefore eliminating the need of the try/catch statements? Am I missing something here? Thanks!
    What code do you write to predict a hardware device fails, or the network being interrupted, or power going out, or even things like users giving bad input? The try/catch IS the way we handle these situations. You expect this thing the code is doing to work, but sometimes it just does not work out, and the code needs to respond.
    For example, you go to the ATM and withdraw money, and there was no try/catch. Suppose the cash dispenser tears up a bill and fails to give out cash. The program told it to give your cash and deducts the amount from your balance leaving you with no cash. On the other hand with the try/catch, when the dispenser fails and throws the exception the program can prevent itself from adjusting your balance knowing no cash was given.

  4. The Following User Says Thank You to jps For This Useful Post:

    LinuxPower (September 6th, 2013)

  5. #4

    Default Re: Exception Handling

    Sounds a bit more clear now. Thanks!

  6. #5
    Grand Poobah
    Join Date
    Mar 2011
    My Mood
    Thanked 167 Times in 158 Posts

    Default Re: Exception Handling

    There are some situations where you can code to avoid an exception rather than use a try/catch statement.
    if(denom != 0) {
        result = num / denom;
    But if you wish to call a method that throws an exception you have no choice but to use try/catch. "So why does that method throw an exception?" I hear you ask. Because the author has no way to determine the almost infinite ways that something can go wrong nor anticipate how you want the code to perform if it does go wrong. Therefore the method throws the exception and whoever called it can handle it however they want.
    Improving the world one idiot at a time!

  7. The Following 2 Users Say Thank You to Junky For This Useful Post:

    jps (September 3rd, 2013), LinuxPower (September 6th, 2013)

Similar Threads

  1. Help with exception handling.
    By K0414 in forum Exceptions
    Replies: 3
    Last Post: April 19th, 2013, 02:18 PM
  2. need help in exception handling
    By gauravdudeja in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2013, 06:14 AM
    By awood in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 3rd, 2013, 04:22 PM
  4. Exception handling
    By AnithaBabu1 in forum Exceptions
    Replies: 6
    Last Post: August 27th, 2008, 09:37 AM