?
Solved

Can I wrap an entire function into a try catch block in java?

Posted on 2005-04-05
10
Medium Priority
?
259 Views
Last Modified: 2012-05-05
Hello experts,

If I want to toss on some try catch statements here.. what is the best way to do that?

Should I try catch the entire function?  Is that even possible?

If not, where and why whould it make sense to place the statements in another location in the funciton?

I am really just trying to lear the basics of Try catch.

********************************************************
    public void transmogrification(double x)
    {
        if (x%ID == 0)    
        {
            if (ID%2 == 0 && transMog_count > 1)
                level = 1;
            else
                level++;
                transMog_count++;
        }
    }

************************************************************

Thank you,

-TH
0
Comment
Question by:travishaberman
10 Comments
 
LVL 15

Expert Comment

by:aozarov
ID: 13710333
>> Should I try catch the entire function?  Is that even possible?
Your method can declare throws <TYPE_Of_EXCEPTION> and then you catch in where you call it.

What are trying to catch here?  RuntimeException ?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13710343
0
 
LVL 15

Assisted Solution

by:aozarov
aozarov earned 600 total points
ID: 13710369
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!

 

Author Comment

by:travishaberman
ID: 13710378
I am not sure what I might want to catch.. a possible division by zero.. or a type mismatch in the double funciton argument..  

Also,, I am not sure what you mean by this.. I am both new to java, and new to exception handling...  

>> Your method can declare throws <TYPE_Of_EXCEPTION> and then you catch in where you call it

Thanks,

-TH
0
 
LVL 10

Accepted Solution

by:
makerp earned 1000 total points
ID: 13710434
void function()
    throws Exception{

    if(bad_condition){
        throw new Exception("something bad");
    }

}

...
try{
    function();
}
catch(Exception e){
    System.out.println(e.toString());
}
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13710440
There is nothing in your method "transmogrification" that throws non runtime exception.
Basically there are two types of exception in java: declared ones and runtime.
You are enforced by the compiler to handle declared exception but not runtime exception.
So, the compiler will not enforce to have any try ~ catch for your method.
Read the articles above, but maybe you want to start with: http://journals.ecs.soton.ac.uk/java/tutorial/java/nutsandbolts/exceptions.html
0
 
LVL 16

Assisted Solution

by:imladris
imladris earned 400 total points
ID: 13710496
Note that most of the common runtime exceptions are inherently generated by Java. The runtime exceptions (that is all exceptions derived from RuntimeException) do not need to be declared or caught.

A divide by 0 problem, for instance, will always generate an ArithmeticException, whether you declare or handle anything or not. You have the option, of course, of catching it if you want. In your above example you could catch, and attempt to handle, a divide by 0 error simply by including a try catch block around the function call:

try
{   transmogrification(d);
} catch(ArithmeticException e)
{   // handle problem
}

If you don't handle it, the exception will simple bubble up the stack, and eventually get to the JVM which will then abort with a suitable error message, listing the details the exception generated.
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13710579
imladris, what you are describing is right but you example is not:

>> A divide by 0 problem, for instance
Not true for double/float
bsh % double y = 5;
bsh % y / 0;
<Infinity>
0
 

Author Comment

by:travishaberman
ID: 13710645
Thank you all!

-TH
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 13710723
Generally I follow this approach

On true condition i return positive value, on false I return 0 and on Exceptional case I return negative integer

When Exception occurs, I log the exception object information into some other objects and these objects can be called in caller method to analyse the exceptional problem


 public int  somemethod(double x)
    {
               try
               {
                         if(somecondition)
                         {
                              << CODE>>
                               return 1;
                         }    
                         else
                          {
                               << CODE>>
                                return 0;
                          }
               }
               catch(Throwable ex)
               {
                    System.err.println("Exception Object is an Instance of :"+ex.getClass());  
                    System.err.println("Exception Message : "+ex);
                    ex.printStackTrace();  
                     <<Some Other Log Objects>>

                      return -1;
               }
               
    }


Now in caller I will call like this
int n=object.somemethod(10);
if(n==-1)
{
   object.getLogObjects();
}
else if(n==1)
{
  //true condition is satisfied
  //so do some works related to that condition
}
else
{
    //false condition is satisfied
  //so do some works related to that condition
}

Regards
Sudhakar Chavali
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month16 days, 23 hours left to enroll

864 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