nightowl_nicky
nightowl_nicky

Reputation: 361

Exception raised within a try/catch block (Java)

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.

Upvotes: 1

Views: 3134

Answers (2)

explv
explv

Reputation: 2759

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

Upvotes: 1

Jordan Kuhn
Jordan Kuhn

Reputation: 103

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.

Upvotes: 3

Related Questions