answersLogoWhite

0

Checked and unchecked exceptions

Updated: 8/11/2023
User Avatar

Wiki User

13y ago

Best Answer

Unchecked exceptions : * represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes : "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time." * are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException * a method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so) Checked exceptions : * represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files) * are subclasses of Exception * a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow) It is somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked).

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Checked exceptions are those that are handled inside the code using a try-catch block. Unchecked exceptions are those that are not and should not be handled using a try-catch block.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Checked and unchecked exceptions
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.


Is it essential to catch all the exceptions and justify your answer?

Checked exceptions should be caught. Otherwise, compile errors are generated.


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 & handled in our code. Ex: I/O Exception, SQL Exception etc. In most cases, the compiler itself forces us to catch & handle these exceptions * Un-checked Exceptions - The ones that we cannot & 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 & caught and handled by try-catch blocks. Ex: try { ..... ..... } catch (Exception e){ ... } finally { ... }


If a method calls another method and that method throw an exception then caller method must be throw that exception or not?

If method A calls method B and method B throws an exception, then method A must handle that exception. It does not have to throw the exception if it is in a try-catch block, but it must do something to deal with it.Note that this only applies to checked exceptions. If method B throws an unchecked exception, then A is allowed to ignore it.


Does throws throws only unchecked exception?

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

Related questions

What are the types of exception in java?

Exceptions are of two types: checked exceptions and unchecked exceptions.


Difference between checked and unchecked exception?

Checked exceptions are exceptions which need to be handled explicitly. These are the ones which require a try-catchblock or a throws keyword.Unchecked exceptions are exceptions which have no obligation to be handled. A NullPointerException is one common example.


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.


Is it essential to catch all the exceptions and justify your answer?

Checked exceptions should be caught. Otherwise, compile errors are generated.


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 & handled in our code. Ex: I/O Exception, SQL Exception etc. In most cases, the compiler itself forces us to catch & handle these exceptions * Un-checked Exceptions - The ones that we cannot & 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 & caught and handled by try-catch blocks. Ex: try { ..... ..... } catch (Exception e){ ... } finally { ... }


What is checked exception?

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.


Can any one please give some example of caught-exceptions and uncaught-exceptions in java?

// Open a reader from standard in... final BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { // BufferedReader.readLine can throw an IOException, which is a checked exception. // So we need to catch it. String str = in.readLine(); // Integer.valueOf throws a NumberFormatException, which is an unchecked exception. // Note that we don't need to catch this. int num = Integer.parseInt(str); } catch (final IOException ex) { }


If you block a picture by mistake in FACEBOOK how do you unblock it?

here's what to do:1- Click 'Tools' in the bar on top.2- Click 'Options' from the Tools button3- Click 'Content'4- If the 'Load Images Automatically' checkbox is unchecked, check it.or, If it is already checked, click 'Exceptions' and click 'Remove all sites'after that, Close it and the pictures can load perfectly in facebook.


If a method calls another method and that method throw an exception then caller method must be throw that exception or not?

If method A calls method B and method B throws an exception, then method A must handle that exception. It does not have to throw the exception if it is in a try-catch block, but it must do something to deal with it.Note that this only applies to checked exceptions. If method B throws an unchecked exception, then A is allowed to ignore it.


How to fix laptop shutting off in normal mode but not in safe mode?

Windows + R” key and then type “msconfig” (without quotes) in the box and then press Enter to open Windows System Configuration. 2. Under Boot tab, make sure the Safe Mode option is unchecked. If it is checked, unchecked it and apply the changes to see if you can boot Windows


Can you receive an email if firewall is set with no exceptions?

Yes, because your e-mail is not on the hardware nor does it involve a port. It is on the net and can be checked from any computer


Does throws throws only unchecked exception?

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