Thursday, March 19, 2020
The try-catch-finally Blocks in Java
The try-catch-finally Blocks in Java To make a Java program as robust as possible it needs to be able to handle exceptions. The compiler does its part by not allowing you to compile a program until it is syntactically correct and can also point out checked exceptions that must be handled. But the exceptions that are likely to cause the most headaches are the ones that appear once the program is running. To help handle these exceptions the Java language provides the try-catch-finally blocks. The try Block The tryblock encases any statements that might cause an exception to occur. For example, if you are reading data from a file using the FileReader class, its expected that you handle the IOExceptions associated with using a FileReader object, for example,FileNotFoundExceptionand IOException. To ensure this happens, you can place the statements that deal with creating and using the FileReader object inside a try block:However, the code is incomplete because, in order for the exception to be handled, we need a place for it to be caught. This happens in the catch block.The catch Block The catch block(s) provide a place to handle the exception thrown by the statements within a try block. The catch block is defined directly after the try block. It must specify the type of exception it is handling. For example, the FileReader object defined in the code above is capable of throwing a FileNotFoundException or an IOException. We can specify two catch blocks to handle both of those exceptions:In the FileNotFoundExceptioncatchblock we could place code to ask the user to find the file for us and then try to read the file again. In the IOException catch block, we might just pass on the I/O error to the user and ask them to try something else. Either way, we have provided a way for the program to catch an exception and handle it in a controlled manner.In Java SE 7, it became possible to handled multiple exceptions in one catch block. If the code we wanted to place in the two catch blocks above was exactly the same we could write the code like this instead:In order to do a bit of housekeeping as far as resources go, we can add a finally block. After all, we want to release the file we have been reading from once we are finished. The finally Block The statements in the finally block are always executed. This is useful to clean up resources in the event of the try block executing without an exception and in the cases when there is an exception. In both eventualities, we can close the file we have been using. The finally block appears directly after the last catch block:
Monday, March 2, 2020
The Case of the Misplaced Modifier
The Case of the Misplaced Modifier The Case of the Misplaced Modifier The Case of the Misplaced Modifier By Carla Lowe As a freelance editor and proofreader, the misplaced or dangling modifier is a common writing error I see. That sentence should have made you cringe, or at least confused. It introduces an all-too-frequent grammatical problem found in writing: misplaced and dangling modifiers. First, what’s a modifier? A modifier is a word, phrase, or clause that â€Å"modifies†(says something about, describes, clarifies, qualifies, limits) another word, phrase, or clause. Modifiers are either adjectival (modifying nouns or pronouns) or adverbial (modifying verbs, adjectives, or other adverbs). The challenge with modifiers lies in knowing where to place them. A misplaced modifier can create confusion and drastically alter the meaning of a sentence because it appears to modify the wrong thing. Misplaced Words Many single-word modifiers are often misplaced; these include only, almost, just, even, merely, hardly, and nearly, for example. Consider how the meaning of the following sentence changes, depending on where you place the word only: Only Susie gave $20 at the fundraiser. (No one else but Susie gave $20.) Susie only gave $20 at the fundraiser. (The only thing Susie did was give $20.) Susie gave only $20 at the fundraiser. (Susie didn’t give any more than $20.) Susie gave $20 at the only fundraiser. (Susie gave $20 at the only available fundraiser.) Susie gave $20 at the fundraiser only. (Susie didn’t give $20 anywhere else.) One trick to help you avoid misplacing these types of modifiers is to place the modifier next to (or near) the word/words it modifies. For the above sentence, if you want only to modify $20, then use the third example. Misplaced Phrases/Clauses Phrases and clauses are also regularly misplaced in writing. Examples: My husband asked me if we might consider having another baby during our friends’ baby shower. (implies that the husband wants to have a baby during the shower) I found a new scratching post for my cat, which was on clearance at the pet store. (implies that the cat was on clearance) To correct these sentences, place the modifying phrase/clause closer to the word/words it modifies: During our friends’ baby shower, my husband asked me if we might consider having another baby. I found a new scratching post, which was on clearance at the pet store, for my cat. Dangling Modifiers A pet peeve of mine, the dangling modifier is usually a phrase or an elliptical clause (a dependent clause in which some words have intentionally been left out), often at the beginning of a sentence, that either doesn’t modify anything specific in the sentence or modifies the wrong word or part of the sentence. Consider my introductory sentence: As a freelance editor and proofreader, the misplaced or dangling modifier is a common writing error I see. Of course, you can probably figure out what I was trying to communicate, but the dangling modifier is distracting and creates ambiguity; it illogically implies that the modifier is actually a freelance editor and proofreader! So how can I fix the sentence? As a freelance editor and proofreader, I notice that many writers struggle with misplaced and dangling modifiers. Now, the introductory phrase (As a freelance editor and proofreader) correctly and logically modifies the pronoun I. Remember the trick to place the modifier as close as possible to whatever it needs to modify, and you’ll avoid the majority of errors associated with modifier placement. Want to improve your English in five minutes a day? Get a subscription and start receiving our writing tips and exercises daily! Keep learning! Browse the Grammar category, check our popular posts, or choose a related post below:How Many Tenses in English?10 Types of TransitionsComma After Introductory Phrases
Subscribe to:
Posts (Atom)