• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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

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
travishaberman
Asked:
travishaberman
3 Solutions
 
aozarovCommented:
>> 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
 
aozarovCommented:
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
travishabermanAuthor Commented:
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
 
makerpCommented:
void function()
    throws Exception{

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

}

...
try{
    function();
}
catch(Exception e){
    System.out.println(e.toString());
}
0
 
aozarovCommented:
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
 
imladrisCommented:
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
 
aozarovCommented:
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
 
travishabermanAuthor Commented:
Thank you all!

-TH
0
 
sudhakar_koundinyaCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now