Question 1 Explain Exception Handling 1What are the rules fo

Question 1: Explain Exception Handling

1.What are the rules for what must be included with a try-block (e.g., catch, finally, etc.)?

2.What is the difference between a checked exception and an unchecked exception?

3.What is the difference between the keyword throws and throw? (This is a classic interview question.)

4.How do you write your own classes to describe erroneous situations? How do you use these classes?

Solution

1 ans: Try Block contains a block of program statements within which an exception might occur. A try block is always followed by a catch block, which handles the exception that occurs in associated try block. A try block must followed by a Catch block or Finally block or both.

Catch Block must be associated with a try block. The corresponding catch block executes if an exception of a particular type occurs within the try block.

Finally Block:

2 ans:The Main difference between Checked and Unchecked Exception lies in there handling. Checked Exception requires to be handled at compile time using try, catch and finally keywords or else compiler will flag error. This is not a requirement for Unchecked Exceptions.

3ans :The difference between the keyword throws and throw are the keyword may look quite similar, especially if you are new to Java programming and haven\'t seen much of it. Though they are similar in terms that both are used in Exception handling, they are different on how and where they are used in code. throws keyword is used in method signature to declare which checked exception method can throw, you can also declare unchecked exception, but that is not mandatory by compiler. This signifies lot of things like method is not going to handle Exception instead its throwing it, if method throws checked Exception then caller should provide compile time exception handling etc.
4 ans: The method\'s signature informs the programmers that an exceptional condition \"file not found\" may arise. By declaring the exceptions in the method\'s signature, programmers are made to aware of the exceptional conditions in using the method. If a method declares an exception in its signature, you cannot use this method without handling the exception then you can\'t compile the program.

Question 1: Explain Exception Handling 1.What are the rules for what must be included with a try-block (e.g., catch, finally, etc.)? 2.What is the difference be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site