Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Is there any case where will not be able to see the error message unless we use try catch block?

 
Ranch Hand
Posts: 1545
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was working on a code which was not working but was not giving any error message either. I put a try catch block and after that I started seeing the error message as it was going to the catch block. I was thinking that if the code was not having try catch than the default handler would show this exception in the console . But like I said this was not showing up. Is there any case where will not be able to see the error message unless we out try catch block.
Thanks
 
Saloon Keeper
Posts: 12165
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's likely that the exception was caught in a higher call frame, but the catch clause was empty. If so, let it be a lesson: NEVER write empty catch clauses.
 
Monica Shiralkar
Ranch Hand
Posts: 1545
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It's likely that the exception was caught in a higher call frame, but the catch clause was empty. If so, let it be a lesson: NEVER write empty catch clauses.



Thanks. Actually, my question is somewhat different from this.

My question is that if I do not put any try catch block, then :

Expected result is that the default handler will show this error at console before stopping abruptly.

Actual results is that it doesn't do so.

So is the result I am expecting expected to happen ?


 
Sheriff
Posts: 15815
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What "default handler" are you referring to? Java doesn't have a "default handler" as far as I know.

EDIT: Just noticed that this is in the C#/.Net forum -- I don't know if what I said is true for C#, honestly, but I'd imagine it is. (I could be wrong, of course)
 
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume C# doesn't have default handlers either; remember this isn't a Java® forum.
 
Monica Shiralkar
Ranch Hand
Posts: 1545
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:What "default handler" are you referring to? Java doesn't have a "default handler" as far as I know.

EDIT: Just noticed that this is in the C#/.Net forum -- I don't know if what I said is true for C#, honestly, but I'd imagine it is. (I could be wrong, of course)



In case of Java , I had read that there is a default exception handler method, which is called as a final handler to take care of any exceptions not caught by the thread in the run() method.

 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's have a look in the JLS (=Java® Language Specification). Please read the link for the gory details. There is a standard way for the thread to terminate; it will eventually call this method.
 
Monica Shiralkar
Ranch Hand
Posts: 1545
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It calls the uncaught exception in that case.

If we have the code within try catch block , the exception will be caught.

If we do not have the code within try catch block ,although the program will terminate it will still show exception message on the console.

Is that correct ?
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do the two links say?
 
Stephan van Hulst
Saloon Keeper
Posts: 12165
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your C# code, what exception was thrown, and how do you know that nothing happened?
 
Monica Shiralkar
Ranch Hand
Posts: 1545
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:In your C# code, what exception was thrown, and how do you know that nothing happened?



The below code is for posting the message to a queue. However it was neither posting ,nor showing any error on the console.

Simply enclosing this in a try catch block started showing me error message in the catch block saying "partition key " cannot be null.

My doubt is that shouldn't it have shown this error on console (and stopped) when I had not enclosed in try catch block ?

Thanks
 
Marshal
Posts: 3156
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the type of _client?

Which exception are you catching?
 
Stephan van Hulst
Saloon Keeper
Posts: 12165
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhh, the code you wrote uses the await keyword, which can only be used in async methods.

Asynchronous method calls can be executed by other threads than the thread you initiated the call from. If the thread that is executing the asynchronous call is not the main thread, then all bets on it writing uncaught exceptions to the console are off.

See what happens if you remove the async and await keywords, and instead call:
 
Monica Shiralkar
Ranch Hand
Posts: 1545
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Uhh, the code you wrote uses the await keyword, which can only be used in async methods.

Asynchronous method calls can be executed by other threads than the thread you initiated the call from. If the thread that is executing the asynchronous call is not the main thread, then all bets on it writing uncaught exceptions to the console are off.

See what happens if you remove the async and await keywords, and instead call:



Thanks.
I tried to do this way to test that .However there is no Result method availble to call the below


Instead I could see only methods like ConfigureAwait ,GetAwaiter,
So instead I could try the below :




This gave the same result as I had mentioned previously .
 
Stephan van Hulst
Saloon Keeper
Posts: 12165
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, then the return type of your method is a Task and not a Task<T>.

In that case, try _client.SendAsync(message, partitionKey).Wait().
 
Monica Shiralkar
Ranch Hand
Posts: 1545
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Ah, then the return type of your method is a Task and not a Task<T>.

In that case, try _client.SendAsync(message, partitionKey).Wait().



Thanks. The result was exactly as you said .This time it gave unhandled exception "value cannot be null ".

This result was same as my initial expectation.

So , it looks like that this situation (exception not shown when not using try catch ) happens only in case of asynchronous programming .
 
Stephan van Hulst
Saloon Keeper
Posts: 12165
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not entirely true.

The exception is not printed when it is thrown by another thread than the main thread. So the cause of your problem is multithreaded programming, not asynchronous programming.

Asynchronous programs can be executed by a single thread. It is a different concept from multithreaded programming.
 
Monica Shiralkar
Ranch Hand
Posts: 1545
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.How do we know in this case thread is a different one from main thread  ?
 
Stephan van Hulst
Saloon Keeper
Posts: 12165
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can call Thread.CurrentThread to see which thread is currently running.

Note that in an async method, every time you use the await keyword, the code that comes after is potentially run by a different thread than the code that comes before the await. The runtime will try to get the original thread to execute the code, but I don't think it's guaranteed.
 
Saloon Keeper
Posts: 22289
151
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It's likely that the exception was caught in a higher call frame, but the catch clause was empty. If so, let it be a lesson: NEVER write empty catch clauses.



Because if I ever have to deal with that code, someone's going to regret it!  
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic