Java Fundamentals - Lesson 30 - More on Exceptions

preview_player
Показать описание
Are you new to Java development? Do you want to know what to start with? This is a complete stream dedicated to you - the junior developer or future developer.

- Java OCP 11 certification
- Learn Java basics
- Find a junior java developer role

If your target is at least one of the above, then this lesson streaming is for you. Join!

Don't forget to follow me on Twitter @laurspilca or LinkedIn for more posts and discussions.

Рекомендации по теме
Комментарии
Автор

Hey Laur,
I found something interesting in a book:
"In general, wrapping your Java code with try/catch blocks doesn’t have a significant performance impact on your applications. Only when exceptions actually occur is there a negative performance impact, which is due to the lookup the JVM must perform to locate the proper handler for the exception. If the catch block for the exception is located in the same method, the impact is not so bad. However, the further down the call stack the JVM has to go to find the exception handler, the greater the impact becomes."
If you have time, do you think you can share your thoughts about this? If you did already a video on performance impact caused by exception-handling, just paste me the link please.

Great content, probably one of the most underrated Java channels. If you were from the USA you'd had over 100k subs by now.
Thanks.

radub
Автор

Thanks!, As you show with an example, the exception that the try-with resources block will throw first will be caught by the catch block, and the others will end up inside a Throwable array. Now, My concern is if the other exceptions are now being suppressed.why the compiler forces me to provide that type of Exception handler (probably generic type) that can also handle the suppressed exceptions?

AliHassan-bzsk
Автор

Hi Laur, In Example 5 at 39:00 timestamp in video, you show the example of new syntax available from Java 9 where we can declare resource outside and just use the variable containing reference with try(r1) - example of try with resources. Now what if the declaration of the resource itself throws the exception, this time it is outside the try () with resources so where will it be catched or how will it be catched?

naeemkhan
Автор

Hello Laur, I have a question about below example. When we run the Example1 class it prints ":)". But I am not getting this:/ How it prints ":)" without calling the close() method? You said that it's called but where is it called? May be I did not get the concept. You said finally block implicitly called. Is it where it happens? Thank you!

Edit: Sorry, I think I got it. It's done behind the scene. I was little confused about the concept. I think I understood. So when we use try-with-resources statement close() method of the resource will automatically be called. Thank you for the great lessons again!

public class R implements AutoCloseable {

@Override
public void close() throws Exception{
System.out.println(":) ");
}
}

public class Example1 {
public static void main(String[] args) {
try (R r = new R()) { // try - with - resources
// r
} catch (Exception e) {
//
}
}
}

Rocky_
Автор

How would java call stack look like when we return some value from both try/finally or catch/finally (if exception is caught)?

bhaskarmishra
Автор

Hello, sorry for the random question
I was wondering if you did any videos on Localization or Annotations? I've seen that those chapters are also for ocp 11

TSV
Автор

Hi! Can you make a Playlist with Java Fundamentals Lessons? Your Playlists are very random. Thanks!

mihnea
Автор

Hi Laur, In Example 5 at 39:00 timestamp in video, you show the example of new syntax available from Java 9 where we can declare resource outside and just use the variable containing reference with try(r1) - example of try with resources. Now what if the declaration of the resource itself throws the exception, this time it is outside the try () with resources so where will it be catched or how will it be catched?

naeemkhan