1

I just started to learn Java few weeks ago. I read from my textbook that it says: if an exception occurs half-way through executing a block of code that is surrounded by a ‘try’ block and is followed by several ‘catch’ clauses (that have their own code blocks), the remainder of the try block will be skipped and if there is a catch clause that matches the type of exception, then the block associated with the catch clause will be executed. However, what happens if no matching catch clause is present? Nothing will executed or any specific case will happen? I understand it's just a simple question but I can't find any answer of it. Thanks for your help.

3
  • 1
    could you post your code so that we can sorted out? Commented Jun 6, 2016 at 14:47
  • Keep in mind that a try-catch block can have a finally section which is guaranteed to always execute after the block has completed regardless of what transpires inside the block (excluding a call to System.exit() or other critical failure). Commented Jun 6, 2016 at 14:48
  • A little hint on my side... If you are using and IDE like IntelliJ or eclipse, you can let them generate the try-catch block for you, if a method you are calling is explicitly throwing an exception (nullPointer etc. probably not)- e.g. you don't have to worry about any exceptions not being catched. Commented Jun 6, 2016 at 14:55

2 Answers 2

3

If no catch block exists to catch the specified exception, the error is thrown upwards (as if you had no try/catch series around it). If a finally block is present it will of course still be executed.

Sign up to request clarification or add additional context in comments.

Comments

1

I will try and explain this for you.

Here is an example of a method that throws an Exception:

public void anExceptionThrowingMethod() {
    throw new Exception("Uh oh an exception occurred!");
}

If we tried to call this method like this:

anExceptionThrowingMethod();

Your program would crash and you would get the error:

java.lang.IllegalArgumentException: Uh oh an exception occurred!

This is because when we call the method, we have not handled the case where an error occurs. To do this, we use the try{ } catch { } blocks:

try {
    anExceptionThrowingMethod();
} catch(Exception e) {
    System.out.println("We handled the exception!");
}

The program will now no longer crash, and will print:

We handled the exception!

When you run this code, the exception throwing method will throw an exception. That exception will be caught by the catch block, and the stack trace will be printed out. No code after the exception throwing method will be executed:

try {
    anExceptionThrowingMethod();
    // Nothing will be executed after this
} catch(Exception e) {
    // Instead, this catch block will be executed
    System.out.println("We handled the exception!");
}

If you always want to execute some code, even if an exception has occurred, you can use the finally block:

try {
    anExceptionThrowingMethod();
    // Nothing will be executed after this
} catch(Exception e) {
    // Instead, this catch block will be executed
    System.out.println("We handled the exception!");
} finally {
    // This block will always be executed, regardless of whether an exception has occurred.
}

If multiple exception types are present, you can either catch the super class Exception or you can handle each individual exception type separately:

try {
    manyExceptionThrowingMethod();
    // Nothing will be executed after this
} catch (InterruptedException e) {
    // Called when an InterruptedException occurs
    e.printStackTrace();
} catch (IllegalArgumentException e) {
    // Called when an IllegalArgumentException occurs
    e.printStackTrace();
} finally { 
    // This code will always be executed, regardless of whether an exception has occurred.
}

If you do not handle an Exception type, your program will crash when that error occurs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.