Try/finally statements related to Exception Handling

Source: Internet
Author: User

1 javapuzzler 36 a statement or block throws an exception in it, or executes a break or continue for a closed statement, or when a return is executed in the method like this program, it will end unexpectedly. They are called unexpected termination.

 

Package arkblue. javapuzzler. n35; </P> <p> public class indecisive {<br/> Public static void main (string [] ARGs) {<br/> system. out. println (demo-(); <br/>}</P> <p> static Boolean demo-() {<br/> try {<br/> return true; <br/>}finally {<br/> return false; <br/>}< br/>

Print false

Do not use a return, break, continue, or throw to Exit A finally statement block, and do not allow the propagation of a checked exception to a finally statement block.

 

2 Java puzzler 37

Package arkblue. javapuzzler. n37; </P> <p> public class arcane1 {<br/> Public static void main (string [] ARGs) {<br/> try {<br/> system. out. println ("Hello World"); <br/>} catch (ioexception e) {<br/> system. out. println ("I 've never seenprintln fail! "); <Br/>}</P> <p> class arcane2 {<br/> Public static void main (string [] ARGs) {<br/> try {<br/> // if you have nothing nice to say, say nothing <br/>} catch (exception e) {<br/> system. out. println ("This can 'thappen "); <br/>}</P> <p> interface type1 {<br/> void F () throws clonenotsupportedexception; <br/>}</P> <p> interface type2 {<br/> void F () throws interruptedexception; <br/>}</P> <p> interface type3 extends type1, type2 {<br/>}</P> <p> class arcane3 implements type3 {<br/> Public void F () {<br/> system. out. println ("Hello World"); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> type3 T3 = new arcane3 (); <br/> t3.f (); <br/>}< br/>

(1) If a catch clause needs to catch a checked exception of Type E, and its corresponding try clause cannot throw an exception of a certain type E, this is a compilation error.

(2) but the catch clause that captures exception or throwble is legal, regardless of the content of the corresponding try clause.

(3) the set of checked exceptions that a method can throw is the intersection of all types of declarations it applies to, rather than the collection.

 

3 Java puzzler 38

It is very difficult to determine whether a program can assign values to an empty final more than once. In fact, this is impossible. This is equivalent to the classic downtime problem, which is generally considered impossible to solve [Turing 36]. In order to be able to compile a compiler, the language standards adopt a conservative approach in this regard. In a program, an empty final field can be assigned a value only when it is explicitly unspecified. This term provides an accurate but conservative Definition [JLS 16]. Because it is conservative, the compiler must reject programs that prove secure.

Package arkblue. javapuzzler. n38; </P> <p> public class unwelcomguest {<br/> Public static final long guest_user_id =-1; <br/> Private Static final long user_id; <br/> static {<br/> try {<br/> user_id = getuseridfromenvironment (); <br/>} catch (idunavailableexception E) {<br/> user_id = guest_user_id; <br/> system. out. println ("logging in as guest"); <br/>}</P> <p> Private Static long getuseridfromenvironment () <br/> throws idunavailableexception {<br/> throw new idunavailableexception (); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> system. out. println ("User ID:" + user_id ); <br/>}</P> <p> class idunavailableexception extends exception {<br/>}< br/>

4 Java puzzler 40

(1) The system. Exit method stops the current thread and all other threads that died on the spot. The appearance of the finally clause does not allow the thread to continue to execute.

(2 ). You can call system. Halt to stop the VM without shutting down the hook.

Package arkblue. javapuzzler. n39; </P> <p> public class hellogoodbye {<br/> Public static void main (string [] ARGs) {<br/> try {<br/> system. out. println ("Hello World"); </P> <p> runtime. getruntime (). addshutdownhook (New thread () {<br/> @ override <br/> Public void run () {<br/> system. out. println ("hook on shutdown! "); <Br/>}< br/>}); </P> <p> system. exit (0); <br/>}finally {<br/> system. out. println ("Goodbye World"); <br/>}< br/>

 

5 Java puzzler

(1) The first error is that the program uses a terrible loop usage, which relies on access to the array and throws an exception. This idiom is not only hard to read,
The running speed is also very slow. Do not use exceptions for loop control.

(2) & operator has other meanings. In addition to common bitwise AND operators used as integer operands, when used as Boolean operands, its function is overloaded as the logical and operator [jls15.22.2]. This operator is different from the more frequently used condition and operator. The & operator always calculates its two operands, And the & operator calculates false as the operand on its left, the right operand is no longer calculated. [JLS 15.23]

(3) The logical or operator (|) is also accompanied by the conditional or operator (|) [JLS 15.22.2, 15.24]. | The operator always needs to calculate its two operands. | when the operator's left operand is calculated as true, it will not calculate the right operand.

Package arkblue. javapuzzler. n42; </P> <p> public class loop {<br/> Public static void main (string [] ARGs) {<br/> int [] [] tests ={< br/> {6, 5, 4, 3, 2, 1}, <br/> {1, 2 },< br/> {1, 2, 3 },< br/> {1, 2, 3, 4 },< br/> {1 }}; <br/> int successcount = 0; <br/> try {<br/> int I = 0; <br/> while (true) {<br/> If (thirdelementisthree (tests [I ++]) <br/> successcount ++; <br/>}< br/>} catch (arrayindexoutofboundsexception E) {<br/> // no more tests to Process <br/>}< br/> system. out. println (successcount); <br/>}</P> <p> Private Static Boolean thirdelementisthree (INT [] A) {<br/> return. length >=3 & A [2] = 3; <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.