Solved

Unchecked, unchecked exceptions and closures

Posted on 2014-03-25
8
248 Views
Last Modified: 2014-05-02
Unchecked exceptions make your code much simpler. For example, the Spring JDBC Template support can shrink your JDBC code enormously by getting rid of those unsightly try, catch, and finally blocks, and cleanly releasing the database resources like connections, statements, etc. Checked exceptions are also the main reason for Java not having closures till JRE 6.0 as it adds to the complexity.

I was reading as above. I have not clearly undertsood it.
please advise
Any links resources ideas highly appreciated. Thanks in advance
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 39955279
It means that checked exceptions add more lines to your code , because you  use try , catch and finally blocks . You don't have to do this with unchecked exceptions.
Not sure where you picked those lines from , but I think it is in no way suggesting replacement of checked exceptions with unchecked exceptions . If your context requires a checked exception , you HAVE TO go for it.
0
 
LVL 7

Author Comment

by:gudii9
ID: 39956362
what is relation between unchecked exceptions and closures. please advise
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 39958590
Closures is a concept where you can pass some data and a block of code(closure) to a function (library), and the library will execute your closure on the data passed . When you do this , your block of code may throw some checked exceptions and the library has to somehow know how to handle/or pass on all these checked exceptions . This is called Exception Transparency
In general ,  checked exceptions are   not preferred by many as you have to write code to handle them even though they might not be thrown
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Author Comment

by:gudii9
ID: 40007620
It means that checked exceptions add more lines to your code , because you  use try , catch and finally blocks . You don't have to do this with unchecked exceptions.

checked exceptions are   not preferred by many as you have to write code to handle them even though they might not be thrown

Unchecked exceptions do not need lot of code?
Please advise
0
 
LVL 7

Author Comment

by:gudii9
ID: 40007796
Common scenarios of Exception Handling where exceptions may occur
There are given some scenarios where unchecked exceptions can occur. They are as follows:
1) Scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.

    int a=50/0;//ArithmeticException  

2) Scenario where NullPointerException occurs
If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.

    String s=null;  
    System.out.println(s.length());//NullPointerException  

3) Scenario where NumberFormatException occurs
The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.

    String s="abc";  
    int i=Integer.parseInt(s);//NumberFormatException  

4) Scenario where ArrayIndexOutOfBoundsException occurs
If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:

    int a[]=new int[5];  
    a[10]=50; //ArrayIndexOutOfBoundsException


http://www.javatpoint.com/exception-handling-and-checked-and-unchecked-exception

Above unchecked exceptions also needs  more lines to code , because we  use try , catch and finally blocks?
please advise
0
 
LVL 12

Accepted Solution

by:
Sharon Seth earned 500 total points
ID: 40012060
As you said what you gave above are all unchecked exceptions and you SHOULD not handle them . These exceptions should , in almost all cases , be thrown and cause the application to stop whatever it was doing at that point , Or they should be handled gracefully . Encountering one of those exceptions means you have hit an unexpected situation , which needs to be analysed . On the other hand , if you just handle and ignore those exceptions , then you it means a possible bug in the system goes uncaught/ignored.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40020541
how is handling exception and 'thrown so that application stops' are different. please advise
0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 40036872
Handling exception , most of the times is where you know that there is an exception , do whatever needs to be done in this situation , and go take the next course of action. The next course of action may be continuing with the execution of the rest of the program (that's what I meant when I said handle an exception) , or  very rarely halt and then throw , so that the execution stops.
What all I meant to say is , never handle/gobble up a runtime exception since a runtime exception clearly indicates an error
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jboss 7.1 start up error 1 83
Java basic valueOf question 1 50
Fast way to search item into Java Array (Rhino compatible) 2 47
Java 8 to Java 6 8 43
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question