Forums Register Login

throw execption in try -catch

+Pie Number of slices to send: Send
How can we throw user defined exception in try catch block ??

sth like:
try {
System.out.println("Hello " + args[0]);
}
catch (ArrayIndexOutOfBoundsException e) {
throw new Exception (" Error.");
}

Is it possible ?
+Pie Number of slices to send: Send
yes it is possible.

cheers!
+Pie Number of slices to send: Send
Yes, it's ok. But you should understand the effect. You hand-over the exceptions to others and it would be like a snow ball. From the design point-of-view, it's ok if you throw the exception in try-catch block to your own exception class, not to general Exception.
+Pie Number of slices to send: Send
Hi ranchers,

Yes, you can do that. But when you rethrow a checked exception you either have to declare in the method signature that the methods will throw this Exception.
Or you have to catch it back again, looks really ugly:

Compiles and prints out
start
main ready
java.lang.ArrayIndexOutOfBoundsException: 0
at Main.main(Main.java:5)




Yours,
Bu.
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 559 times.
Similar Threads
Exceptions
Regarding setLoginTimeout(int seconds) in JDBC
Unable to create JAXBContext
How to throw exceptipn through servlet.
Casting Throwbale to Exception
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:23:02.