Hi,
Can try-catch statement can only be used to handle checked exceptions?
&&
Are exceptions objects?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
Can try-catch statement can only be used to handle checked exceptions?
&&
Are exceptions objects?
A catch can handle any type of exception, checked or unchecked.
Yes, apart the fact that an exception is in a well defined hierarchy (with java.lang.Throwable at the "top"), an exception is an object like others. When you throw an exception you typically do something like:
throw new XyzException();
Thus, you first instantiate the exception like any other class.
Andrea, www.andbin.net — SCJP 5 (91%) – SCWCD 5 (94%)
Useful links for Java beginners – My new project Java Examples on Google Code
GregBrannon (January 3rd, 2014), jamesh (January 3rd, 2014)
Yes. Read up on them a little at Essential Classes > Exceptions.Originally Posted by jamesh
I'm not sure what you mean by 'checked exceptions' but if you remove the word 'checked' then yes. The purpose of a try-catch block is to react to code that may throw an exception.Can try-catch statement can only be used to handle checked exceptions?
try { // Some code that throws an exception // Note that if an exception *is* thrown at some point // it doesn't execute any more code from this block } catch (NameOfTheException) { // Do something when this type of exception is thrown } catch (Exception) { // Exception is the superclass of all exceptions. // You can even create your own exceptions by extending this class // and then throw it where appropriate } finally { // This happens weather an exception is thrown or not. }
jamesh (January 3rd, 2014)