Forums Register Login

Exception

+Pie Number of slices to send: Send
Hi,
I am bit confused. Tell me if a block of code doesnot throw any exception, its valid to enter in try and catch block right. But sometime back, when I wrote some code, it gave me compiler error, saying that it is not throwing any exception, so there is no need of try or catch block. It will be good if somebody explains me with code.
Thanks in advance
+Pie Number of slices to send: Send
Arathi :
I believe, the code you write inside
your try{} block must throw the kind of exception that you are catching in your catch block.
If you write

then the compiler will complain.
This is the message I get
Foo.java:9: Exception java.io.IOException is never thrown in the body of the corresponding try statement.
catch (IOException e){ // some exception catching code here
^
HTH
[ January 16, 2002: Message edited by: Shivaji Marathe ]
[ January 16, 2002: Message edited by: Shivaji Marathe ]
+Pie Number of slices to send: Send
This rule doesn't apply for a non checked exception.
However an instance of Exception can be caught without being thrown.
+Pie Number of slices to send: Send
 

Originally posted by Arathi Rajashekar:
Hi,
I am bit confused. Tell me if a block of code doesnot throw any exception, its valid to enter in try and catch block right. But sometime back, when I wrote some code, it gave me compiler error, saying that it is not throwing any exception, so there is no need of try or catch block. It will be good if somebody explains me with code.
Thanks in advance



If you catch an non-checked exception , without throwing it or calling/doing anything that can throw it in your try block then you will get a compile time error

However if the exception is non-checked like nullptr or class cast then it doesnt matter wheather you catch or not , it will still compile becoz the programmer is not obligated to do it at compile time
Ragu
+Pie Number of slices to send: Send
hi all,
that was a good point. i didn't know that.
regards
maulin
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 561 times.
Similar Threads
Query
Exception problem
Illegal Argument Exception
not able to understand the output of the following piece of code ?
try-catch-finally
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:18:37.