answersLogoWhite

0


Best Answer

though catching exceptions is a good practice inside java code, catching all exceptions of the type exception is not the best way to go. Specific exceptions need to be caught instead of the generic Exception being caught. Also, different types of exceptions need to be handled separately.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the implication of catching all the exceptions with the type Exception?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is unchecked exception?

A checked exception is an exception which the Java source code must deal with, either by catching it or declaring it to be thrown. Unchecked exceptions are all exceptions which do not follow this rule. When an unchecked exception is thrown, it is usually caused by a misuse of code - passing a null or otherwise incorrect argument. This includes classes like NullPointerException and IllegalArgumentException. Checked exceptions are generally caused by faults outside of the code itself - missing resources, networking errors, and problems with threads come to mind. These could include subclasses of FileNotFoundException, UnknownHostException, etc. The Java documentation (link below) gives some loose guidelines to follow when trying to decide which type of exception to use: "If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception." From a purely code-oriented point of view, a checked exception is a subclass of Exception, while an unchecked exception is a subclass of RuntimeException.


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

A try block can have multiple catch blocks, each handling a different type of exception. Be careful about the order of laying the exceptions. Using Exception at the top will catch everything that is derived from it, thereby missing the other specfic exception catches. And there can also be only one finally block.


Does throws throws only unchecked exception?

You can throw any type of exception you want, including an unchecked exception.


Write a program to demonstrate the catching of all exceptions what happens when a raised exception is not caught by catch block in the absence of a catch all exception block?

If an exception is not caught then your program has undefined behaviour. Ultimately the program crashes, but since you haven't handled the exception you've no way of knowing what damage has been done. Files could be wiped or currupted, planes could fall from the sky... anything is possible with undefined behavour. The best way to deal with unknown exceptions is to first catch them with a catch-all. However, you cannot actually handle the exception unless you know what type of exception you are actually dealing with. Thus the normal course of action is to assume the worst, perform any and all necessary cleanup, log the exception as an unknown exception and rethrow. As the exception percolates back down the call stack, all other exception handlers should do the same: cleanup, log and rethrow. If you're lucky, another handler might recognise the exception and be able to provide more detailed information on the type of exception. Ultimately you must never allow a program to continue executing if you cannot handle an exception. C++11 offers a more elegant solution using a nested try catch within a catch-all. However, it makes more sense to place the nested try catch in a global function (e.g., handle_eptr()) which can specifically deal with all catch-all exceptions. Like so... #include <iostream> #include <string> #include <exception> #include <stdexcept> void handle_eptr(std::exception_ptr eptr) { try { if (eptr) std::rethrow_exception (eptr); } catch (const std::exception& e) { std::cerr << "Exception: "" << e.what() << ""\n"; } } int main() { try { std::string().at(1); // throws "invalid string position" } catch(...) // catch-all { // perform any necessary cleanup here before calling the global handler handle_eptr (std::current_exception()); } } Output: Exception: "invalid string position" Note that handle_eptr() receives a std::exception_ptr. By itself this is useless, but std::rethrow_exception() converts the eptr to a standard exception which can then be thrown, caught and logged. You must still perform any necessary cleanup and allow the program to terminate gracefully, but at least you now know what type of exception you are dealing with and can provide a specific handler to handle it. Note that handle_eptr() should be fleshed out to accept all necessary debug information such as the filename and the line number from the catch_all that called it. The example merely demonstrates how to pass the eptr and convert it into an actual exception.


What is class cast exception?

The class case exception is thrown when an object A of class type B is cast to a class type C where C is neither B nor its subclass.

Related questions

What is unchecked exception?

A checked exception is an exception which the Java source code must deal with, either by catching it or declaring it to be thrown. Unchecked exceptions are all exceptions which do not follow this rule. When an unchecked exception is thrown, it is usually caused by a misuse of code - passing a null or otherwise incorrect argument. This includes classes like NullPointerException and IllegalArgumentException. Checked exceptions are generally caused by faults outside of the code itself - missing resources, networking errors, and problems with threads come to mind. These could include subclasses of FileNotFoundException, UnknownHostException, etc. The Java documentation (link below) gives some loose guidelines to follow when trying to decide which type of exception to use: "If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception." From a purely code-oriented point of view, a checked exception is a subclass of Exception, while an unchecked exception is a subclass of RuntimeException.


What is meant by error handling support in dot net?

The idiomatic way to express error conditions in .NET framework is by throwing exceptions. In C#, we can handle them using the try-catch-finally statement:try {// code which can throw exceptions}catch{// code executed only if exception was thrown}finally{// code executed whether an exception was thrown or not}Whenever an exception is thrown inside the try block, the execution continues in the catch block. The finallyblock executes after the try block has successfully completed. It also executes when exiting the catch block, either successfully or with an exception.In the catch block, we usually need information about the exception we are handling. To grab it, we use the following syntax:catch (Exception e) {// code can access exception details in variable e}The type used (Exception in our case), specifies which exceptions will be caught by the catch block (all in our case, as Exception is the base type of all exceptions).Any exceptions that are not of the given type or its descendants, will fall through.We can even add multiple catch blocks to a single try block. In this case, the exception will be caught by the first catch block with matching exception type:catch (FileNotFoundException e) {// code will only handle FileNotFoundException}catch (Exception e){// code will handle all the other exceptions}This allows us to handle different types of exceptions in different ways. We can recover from expected exceptions in a very specific way, for example:If a user selected a non-existing or invalid file, we can allow him to select a different file or cancel the action.If a network operation timed out, we can retry it or invite the user to check his network connectivity.For remaining unexpected exceptions, e.g. a NullReferenceExceptions caused by a bug in the code, we can show the user a generic error message, giving him an option to report the error, or log the error automatically without user intervention.


What would you have in your if you've caught a Pulex irritans?

What you would have in your hand when catching a pulex irritants is a human flea. This type of flea is found all over the world with the exception of the Arctic region.


What would you have in your hand if you've caught a Pulex irritans?

What you would have in your hand when catching a pulex irritants is a human flea. This type of flea is found all over the world with the exception of the Arctic region.


What would you have in your hands if you've caught a Pulex irritans?

What you would have in your hand when catching a pulex irritants is a human flea. This type of flea is found all over the world with the exception of the Arctic region.


What would you have in your hand if you've caught pulex irritans?

What you would have in your hand when catching a pulex irritants is a human flea. This type of flea is found all over the world with the exception of the Arctic region.


Are air horns legal in the UK?

It is illegal to use any type of horn that emits more than one tone, unless the tones are simultaneous. There are no exceptions. Emergency vehicles on route are an exception.


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

A try block can have multiple catch blocks, each handling a different type of exception. Be careful about the order of laying the exceptions. Using Exception at the top will catch everything that is derived from it, thereby missing the other specfic exception catches. And there can also be only one finally block.


Does throws throws only unchecked exception?

You can throw any type of exception you want, including an unchecked exception.


What type of eukaryotes are protists with the exception of algae?

Protists, with the exception of algae, are unicellular.


Can Empoleon learn fly?

Empoleon is not able to learn the HM move Fly. Typically only Flying type Pokemon can learn the move Fly with only a small handful being the exception. And most of those exceptions have the ability Levitate.


What With the exception of algae all protists are what type of eukaryotes?

Protists, with the exception of algae, are unicellular.