answersLogoWhite

0


Best Answer

In Java it is related to the class hierarchy of exceptions. Throwable is the root object of the heirarchy, and both Exception and RuntimeError subclass it. Methods include a "throws" clause in their signature to indicate errors of type "Exception" that can be thrown in the body of the method and returned to the caller. Errors of type RuntimeError do not need to be reported in the throws clause.


Exceptions are also called "checked" errors, and RuntimeErrors are called "unchecked." This simply means that methods that throw checked errors must declare them in the signature.


* Exception: this is a "checked" error. Usually, this is some sort of data error that can be handled by the method. An example is FileNotFoundException - some code didn't an expected file, but the program may be perfectly capable of going on.

* Error: A more serious problem that a program probably can't deal with. An example is OutOfMemoryError - when there is no more memory, there isn't much you can do about it and not much point in continuing.

* Throwable: Superclass of both Error and Exception.


So the main difference is that

* 'catch Exception' will catch just exceptions (the problems you might deal with)

* 'catch RuntimeError' will catch just errors (the problems you probably can't deal with)

* 'catch Throwable' will catch all errors.


Generally, you want to catch Exception or Throwable, but not RuntimeError. You will only want to catch Throwable if there is a requirement that the method never throw an error back to the caller.


See related link to the Java API, and browse the documentation and subclasses of Throwable.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the difference between catch exception e catch error err and catch throwable t?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between throw and throws in Java?

Throw is used to actually throw the exception, whereas throws is declarative for the method. They are not interchangeable.public void myMethod(int param) throws MyException{if (param < 10){throw new MyException("Too low!);}//Blah, Blah, Blah...} The Throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method.If a method is throwing an exception, it should either be surrounded by a try catch block to catch it or that method should have the throws clause in its signature. Without the throws clause in the signature the Java compiler does not know what to do with the exception. The throws clause tells the compiler that this particular exception would be handled by the calling method.


Describe the JAVA throwable class hierarchy and types of exceptions?

In Java there are two main types of Exceptions. * Checked Exceptions - The ones that can be checked &amp; handled in our code. Ex: I/O Exception, SQL Exception etc. In most cases, the compiler itself forces us to catch &amp; handle these exceptions * Un-checked Exceptions - The ones that we cannot &amp; should not handle in our code. Ex. Null Pointer Exception The java.lang.Throwable is the super class of all errors and exceptions in Java. Only objects of this class can be thrown &amp; caught and handled by try-catch blocks. Ex: try { ..... ..... } catch (Exception e){ ... } finally { ... }


How does a try statement determine which catch clause should be used to handle an exception?

When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception that was thrown, is executed. The remaining catch clauses are ignored


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.


How do you use try block in cpp?

A try statement is used in conjunction with one or more catch blocks to provide exception handling. If an exception is thrown by a try block, the corresponding catch block will handle the exception. If no catch block is provided for a particular exception, then a runtime error occurs instead. Try-catch statements are used to provide graceful resolutions to potential runtime errors.

Related questions

Can you catch multiple throwables in a try catch statement in Java?

Yes, the format of a try/catch/finally block is: try{ // Do Code } catch (Throwable A) { // Process throwable/exception } catch (OtherThrowable B) { // Process throwable/exception } // ... and so on and so forth, catching as many different catches as needed finally{ // Code you always want to execute, whether breaking out of a try // statement normally or by catching a throwable. // For example, close database connections or file handles here. }


What is the difference between throw and throws in Java?

Throw is used to actually throw the exception, whereas throws is declarative for the method. They are not interchangeable.public void myMethod(int param) throws MyException{if (param < 10){throw new MyException("Too low!);}//Blah, Blah, Blah...} The Throw clause can be used in any part of code where you feel a specific exception needs to be thrown to the calling method.If a method is throwing an exception, it should either be surrounded by a try catch block to catch it or that method should have the throws clause in its signature. Without the throws clause in the signature the Java compiler does not know what to do with the exception. The throws clause tells the compiler that this particular exception would be handled by the calling method.


Describe the JAVA throwable class hierarchy and types of exceptions?

In Java there are two main types of Exceptions. * Checked Exceptions - The ones that can be checked &amp; handled in our code. Ex: I/O Exception, SQL Exception etc. In most cases, the compiler itself forces us to catch &amp; handle these exceptions * Un-checked Exceptions - The ones that we cannot &amp; should not handle in our code. Ex. Null Pointer Exception The java.lang.Throwable is the super class of all errors and exceptions in Java. Only objects of this class can be thrown &amp; caught and handled by try-catch blocks. Ex: try { ..... ..... } catch (Exception e){ ... } finally { ... }


What is the difference between tossing and catch?

Tossing is when you throw something and catch is when u catch the toss


Define a catch block?

A Catch block is part of the exception handling mechanism in Java. It is used along with the try block. Ex: try { ... } catch (Exception e) { ... } The catch block is used to catch &amp; handle the exception that gets thrown in the try block.


What is catch net?

When an exception occurs in program execution. Such as 1/0 or divide by zero exception. The program must catch the exception or the program will crash. Although handling of the exception or solution is not necessary.


What is the difference between ruby and sapphire?

the pokemon u catch


What is the difference between a riverclan warrior and a fish?

a fish is harder to catch


What is the use of catch block in java?

the catch block catches the exception from the try block to display a proper message about the exception. Answered by, SOORAJ.M.S


How do you throw exception?

using throws class try, catch block we through the exception


How does a try statement determine which catch clause should be used to handle an exception?

When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception that was thrown, is executed. The remaining catch clauses are ignored


Can you throw exception from catch block in net?

Yes. Use C# code as an example: try { int k = 100 / 0; // this will throw an exception } catch (Exception e) { throw new SystemException("Throwing a new exception because of "+ e.Message); }