Java Exception

Java Exception

Explain about checked and unchecked Exceptions with Examples?

View Answers

August 11, 2012 at 10:47 AM

Checked Exceptions - These are the exceptions which occur during the compile time of the program. The compiler checks at the compile time that whether the program contains handlers for checked exceptions or not. These exceptions do not extend RuntimeException class and must be handled to avoid a compile-time error by the programmer. These exceptions extend the java.lang.Exception class These exceptional conditions should be anticipated and recovered by an application. Furthermore Checked exceptions are required to be caught. Remember that all the exceptions are checked exceptions unless and until those indicated by Error, RuntimeException or their subclasses.

For example if you call the readLine() method on a BufferedReader object then the IOException may occur or if you want to build a program that could read a file with a specific name then you would be prompted to input a file name by the application. Then it passes the name to the constructor for java.io.FileReader and opens the file. However if you do not provide the name of any existing file then the constructor throws java.io.FileNotFoundException which abrupt the application to succeed. Hence this exception will be caught by a well-written application and will also prompt to correct the file name.

Unchecked Exceptions - Unchecked exceptions are the exceptions which occur during the runtime of the program. Unchecked exceptions are internal to the application and extend the java.lang.RuntimeException that is inherited from java.lang.Exception class. These exceptions cannot be anticipated and recovered like programming bugs, such as logic errors or improper use of an API. These type of exceptions are also called Runtime exceptions that are usually caused by data errors, like arithmetic overflow, divide by zero etc.

Lets take the same file name example as described earlier. In that example the file name is passed to the constructor for FileReader. However, the constructor will throw NullPointerException if a logic error causes a null to be passed to the constructor. Well in this case the exception could be caught by the application but it would rather try to eliminate the bug due to which the exception has occurred. You must have encountered the most common exception in your program i.e. the ArithmeticException. I am sure you must be familiar with the reason of its occurrence that is when something tries to divide by zero. Similarly when an instance data member or method of a reference variable is to be accessed that hasn't yet referenced an object throws NullPointerException.









Related Tutorials/Questions & Answers:
Exception Java
Exception Java  Hi, What are the Exception Java? Thanks   Hi, Read it at Java Exception. Thanks
Java exception
Java exception  What happens if an exception is not caught
Advertisements
Java exception
Java exception  Can an exception be rethrown
Java Exception
Java Exception  Which is superclass of Exception
java exception
java exception   define an exception called no match exception tat z... { public static void main(String[] args) throws Exception { String z="Hello"; if(!z.equals("India")){ throw new Exception
Java exception
Java exception  What is the difference between exception and error
Java exception
Java exception  What is the difference between Checked and Unchecked exception
exception in java - Java Beginners
exception in java  exception in java
Java Exception
Java Exception  Explain about checked and unchecked Exceptions... this exception will be caught by a well-written application and will also prompt... to the constructor. Well in this case the exception could be caught by the application
exception in java
exception in java  StringIndexOutOfBounds Exception in flames program   Hi Friend, We have providing you a program. Try it:ADS_TO_REPLACE_1 import java.util.*; public class FlamesProgram{ static String name1
Java Exception
Java Exception  What are monitors
Java Exception
Java Exception  What are Chained Exceptions
Java exception
Java exception  What is NullPointerException and how to handle
Java exception
Java exception  Can try statements be nested
Java Exception
Java Exception  What is the purpose of finalization
Java Exception
Java Exception  Why Runtime Exceptions are Not Checked
Java Exception
Java Exception  What is ?Resource leak
Java Exception
Java Exception  Why Errors are Not Checked
Java Exception
Java Exception  Can a catch block exist without a try block
Java Exception
Java Exception  If I write System.exit (0); at the end of the try block, will the finally block still execute
Java exception handling
Java exception handling  what are the constraints imposed by overriding on exception handling
null pointer exception in java
null pointer exception in java  What is null pointer exception in Java and when it occurs? Show it with an example.Thanks!   Java Null Pointer Exception
java exception handle
java exception handle  How error can be handled in the exception class?? can we handle fatal error in java
Java exception handling
Java exception handling  How does a try statement determine which catch clause should be used to handle an exception
Exception - Java Beginners
What is Exception in Java  What is the Exception that occurs while running the application in Java
Java exception handling
Java exception handling  How does a try statement determine which catch clause should be used to handle an exception
Java exception handling
Java exception handling  What is the catch or declare rule for method declarations
Java exception handling
Java exception handling  What is the difference between throw and throws keywords
Exception - Java Beginners
Chained Exception in Java  What is the Chained Exception in Java?  Chained Exception in JavaChained Exception in Java, this is a new... of exceptions, and these exception can be caused by another exception.Exceptions in Java
exception
exception  chek in and check out exception in java   Please visit the following link: Checked and Unchecked Exception
check null exception java
check null exception java  check null exception java - How to check the null exception on Java?   The null pointer exception in java occurs... on it. See the example null pointer exception
Java exception handling
Java exception handling  What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement
Java Exception - Java Beginners
Java Exception  Why we are using throws in the java program even if we have try...catch block? If throws will throw the exception then who will catch... links: http://www.roseindia.net/java/exceptions/how-to-throw-exceptions.shtml
java question on exception
java question on exception  can we use inbuilt exception variable with throw keyword in java
Exception Handling - Java Beginners
Exception Handling  hi,can u pls make me understand d clear cut difference between throw n throws keyword... n how can we make our own exception..i... want to force an exception then we use throw keyword. the throw keyword is used
Exception Handlerin java..
Exception Handlerin java..  can a catch block accept multiple argument and return type?? please tell with explaination
Java sql Exception
Java sql Exception  difference between sql exception class and sql warning class
Java exception class
Java exception class  What classes of exceptions may be caught by a catch clause
Java exception handling
Java exception handling  Does the order of placing catch statements matter in the catch block
Java exception handling
Java exception handling  What classes of exceptions may be caught by a catch clause
exception in java - Java Beginners
exception in java  exception in java  Hi Friend, Exceptions are the errors that occur during the execution of the program. Exceptions...: http://www.roseindia.net/java/exceptions/ Thanks
Exception - Java Interview Questions
Exception in Java programming  What is Exception?  Chained Exception in JavaChained Exception in Java, this is a new in release 1.4..., and these exception can be caused by another exception. Exceptions in Java
List of Java Exception
List of Java Exception       Exception in Java are classified on the basis of  the exception handled by the java compiler. Java consists of the following type of built
List of Java Exception
List of Java Exception     ...; the exception handled by the java compiler. Java consists of the following type... that defined in the java. lang package.    Exception
Java Mail exception - JavaMail
Java Mail exception  Hi! I am developing E-mail Client (GMAIL... while i am connecting to the Gmail, I am getting the following exception... Whats wrong... I dont know.. How to avoid this exception. The exception
Java Stream Write Exception
Java Stream Write Exception  I am doing socket programming. I have...(Exception e) { System.out.println("Exception :: "+e); } When the line out.writeUTF(data1) is executed and catch catches it and shows the exception
Exception - Java Beginners
://www.roseindia.net/java/java-exception/index.shtml Thanks... (inclusive), you want to throw your own exception. This means you will have to create your own exception class called NotCorrectAgeException and The main
java Exception - Java Beginners
java Exception  Hello sir ,Here Exception in thread "main" java.lang.NoClassDefFoundError: bitwisedemo1/java Help me /** * Author@sushant savant a=64,I=a<<2 find I using Bitwise Left shift * */ public
Exception Handling in java
Exception Handling in java  what is advantage to catch smaller exception first and then normal exception. I mean we normally catch SQLException first and then Exception.   Hi, The exceptions should be from
Exception
Exception  whis is the Arithmetic Exception in java? or define Arithmetic Exception with exp?   Arithmetic Exception occurs, when you divide a number by zero. Example public class TryCatch { public static void main

Ads