answersLogoWhite

0


Best Answer

Well, if at that point you know that an error has occurred, then why not write the proper code to handle that error instead of passing a new Exception object to the catch block? Throwing your own exceptions signifies some design flaws in the project.

I think the bigger point is that if you are catching the Exception superclass, then you have no idea which type of exception is being thrown and will thus not be able to recover from specific problems.

It's almost always better to write this:

try {

InputStream in = new FileInputStream("myfile.txt");

int ch;

while ((ch = in.read()) != -1) {

System.out.print((char) ch);

}

in.close();

} catch (FileNotFoundException ex) {

} catch (IOException ex) {

}

Than it is to write this:

try {

InputStream in = new FileInputStream("myfile.txt");

int ch;

while ((ch = in.read()) != -1) {

System.out.print((char) ch);

}

in.close();

} catch (Exception ex) {

// We have no idea what went wrong here.

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why is catch Exception almost always a bad idea?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Computer Science

What does autonomic computing refer to?

Autonomic computing refers to computers self-managing themselves. Although the idea was always there, IBM created the first one in 2001. The idea was having a computer self-managing with little to no human interactions.


What sort of website does the internet lack?

The Internet has websites for almost any conceivable topic. That being said, if you can come up with a topic that the Internet doesn't have a site for, you could be onto a goldmine of an idea!


What could cause a home computer to get the IP address 169.254.100.88?

The 169.254/16 network is a reserved range of IP addresses, which a client can assign to itself in case a DHCP lease cannot be obtained (RFC 3927). The idea is that, if the DHCP server fails to respond, the requesting device can self-assign a possibly suitable address (such as 169.254.100.88) in order to obtain any usable IP address. In reality, this scheme doesn't really work, however, as the resulting address is almost always on a not supported subnet, and the self-assignment lacks other important details (such as the gateway address).


When you register your original idea or concept with the government for legal protection that idea or concept becomes your?

the answer is i, intellectual property


Mac book and your mom always bitches saying to unplug the charger when im not charging it and im trying to figure out does your charger use power when im not charging the computer?

A charger does use power when not charging the computer if it is plugged in. Not as much, but definitely some. How much depends on many factors. It is a good idea to unplug it.

Related questions

What happens when raised exception is not caught by catch block?

A non-caught exception is propagated out of the local catch block into the next catch block, daisy chaining to the outermost catch block in the run-time library, where it will be handled by abending the program.


How many catch blocks can you use with one try block in java?

Several - I don't believe there is a fixed limit. The idea is that, depending on the specific exception that occured, you carry out different actions.


Do i need to use a Vapor barrier over garage insulation?

A vapor barrier in sidewall insulation is almost always a good idea.


Can unhandled exceptions be tolerated?

Yes, but they dont always result in acceptable or good behavior. Exception handling allows developers to detect errors easily without writing special code to test return values. Even better, it lets us keep exception-handling code cleanly separated from the exception-generating code. It also lets us use the same exception-handling code to deal with a range of possible exceptions. So, it is always a good idea to handle exceptions rather than leave them unhandled


What parts of speech is encountered?

It is almost always a verb, (in the past tense) but in rare cases, it can also be an adjective.


What materials will I need before hanging tech lights in my home?

It is always a great idea to read the instructions sheet that is included with your lights. You almost always get a list of necessary tools need for installation.


Would going to a county fair be a good date for a 12 year old?

Yes That's almost always a good idea. Have a nice date


What is false reassurance?

A pleasant lie. It most often means telling sick people that they're doing good when they're not. That is almost always a bad idea.


What is an exception to the general idea that markets lead to an efficient allocation of resources?

Imperfect Compitition


Where can I read carparts.com reviews online?

You can always find reviews online, which can be most helpful in deciding if you will make a purchase or not, and carparts.com is no exception to that rule. The reviews should give you an idea if you are comfortable buying from them. Don't forget that you can always check with the better business bureau for information in that regards also.


What is an exception to the general idea that markets lead to an efficient allocatoin of resources?

imperfect competition - novanet


What is the main idea of Pokemon?

Gotta catch 'em all!