answersLogoWhite

0

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

15y ago

What else can I help you with?

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.


Is sometimes a connective?

"Sometimes" can be considered a connective when used to introduce a contrasting idea or exception in a sentence. It indicates that something does not always occur or is not the usual case.


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.


Why is catch Exception a bad idea?

Using catch Exception is generally considered a bad practice because it can obscure the handling of specific exceptions and lead to unintended consequences. It may inadvertently catch exceptions that should be handled differently, making debugging difficult and masking underlying issues. Additionally, it can prevent the application from responding appropriately to critical errors, potentially leading to unpredictable behavior or data loss. Instead, it's better to catch specific exceptions to ensure proper error handling and maintain code clarity.


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 an exception to the general idea that markets lead to an efficient allocation of resources?

Imperfect Compitition


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.


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.