473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

unreported exception

281 100+
I have unreported exceptions as below:

unreported exception java.lang.Exception; must be caught or declared to be thrown
Euclid euc = new Euclid();
^
unreported exception java.lang.Exception; must be caught or declared to be thrown
euc.methodEuclid(arrayArgs);
^
2 errors


Below is my code...what's wrong with it..
In my Euclid class already throws exception ...please help me
Expand|Select|Wrap|Line Numbers
  1. try
  2.             {   
  3.                 BufferedReader inputA = new BufferedReader (new FileReader("A.txt"));
  4.                 BufferedReader inputB = new BufferedReader (new FileReader("B.txt"));                   
  5.  
  6.                 for(int ind=0; ind<2;ind++)
  7.                 {
  8.                     lineA = inputA.readLine(); 
  9.                     lineB = inputB.readLine();                       
  10.  
  11.                     String a = lineA;
  12.                     String b = lineB;
  13.                     String[] arrayArgs = new String[] { a, b}; 
  14.  
  15.                     Euclid euc = new Euclid();
  16.                     euc.methodEuclid(arrayArgs);    
  17.  
  18.                }
  19.  
  20.              } catch (FileNotFoundException e) 
  21.               {
  22.                    e.printStackTrace(); 
  23.               }
  24.  
  25.              catch (IOException e) 
  26.            {
  27.               System.out.println("There was a problem creating/writing to the temp file");
  28.               e.printStackTrace();
  29.            } 
Apr 27 '07 #1
7 20144
dmjpro
2,476 2GB
did u specify the method ,where u write the main code .....
like this .....

return_type method_name (args) throws Exception

that's why this error thrown by Compiler.

because the Exception is the base class of all exception classes.

actually here checked and unchecked exception come.

i think u got my point.
Apr 27 '07 #2
shana07
281 100+
did u specify the method ,where u write the main code .....
like this .....

return_type method_name (args) throws Exception

that's why this error thrown by Compiler.

because the Exception is the base class of all exception classes.

actually here checked and unchecked exception come.

i think u got my point.
Yes, I think I got it.
Could you pls tell me how to tackle this problem?
I've tried to remove throws exception at both methods,
but I think it can't be removed (throws exception)
Expand|Select|Wrap|Line Numbers
  1.  public void methodEuclid(String[] arrayArgs)
  2.     {
  3.         int opcode = GCD; 
  4.  
  5.         BigInteger b1=null, b2=null;
  6.         String out="";
  7.  
  8.         b2 = receiveNumber(arrayArgs[1]);
  9.         b1 = receiveNumber(arrayArgs[0]);
Apr 27 '07 #3
dmjpro
2,476 2GB
it ll be better if u post ur total code.

then i can solve ur problem.

plz send.
Apr 27 '07 #4
JosAH
11,448 Expert 8TB
Basically exceptions come in two flavours: checked and unchecked exceptions.
The unchecked exceptions are thrown when a program error occurs such as
null pointers, division by zero, illegal arguments, array index out of bounds etc.

The compiler can't check on these exceptions so it doesn't. The checked
exceptions are usually external events that cause the program to fail such as
IO errors, broken socket connections etc. When a piece of code can throw
a checked exception it has to say so in its method header otherwise the
method can't throw such an exception. It can catch such exceptions and deal
with it (e.g. repair the situation, inform the user about it etc.)

The compiler checks your code for it: i.e. when your method can (in)directly
throw an exception it has to report it in its method header:
Expand|Select|Wrap|Line Numbers
  1. public void foo() throws FooExecption { ... }
If you fail to do that the compiler will flag that as a compilation error. Some
beginners are tempted to just write a "throws Exception" and forget about
the rest. This is a very bad habit because any other code calling your method
can't tell *what* exactly was thrown and act accordingly.

Exceptions must be part of the design, never just put "throws Exception"
at every method header when the compiler complains about it.

kind regards,

Jos
Apr 27 '07 #5
shana07
281 100+
it ll be better if u post ur total code.

then i can solve ur problem.

plz send.
yes please help me to check my code..Thank you very much
The first code is from MyProgram class - I put main method here
Expand|Select|Wrap|Line Numbers
  1. public static void main (String[] args) 
  2.   {
  3.          String lineA = "";
  4.          String lineB = ""; 
  5.          try
  6.             {   
  7.                 BufferedReader inputA = new BufferedReader (new FileReader("A.txt"));
  8.                 BufferedReader inputB = new BufferedReader (new FileReader("B.txt"));                   
  9.  
  10.                 for(int ind=0; ind<20;ind++)
  11.                 {
  12.                     lineA = inputA.readLine(); 
  13.                     lineB = inputB.readLine();                       
  14.  
  15.                     String a = lineA;
  16.                     String b = lineB;
  17.                     String[] arrayArgs = new String[] { a, b}; 
  18.  
  19.                     Euclid euc = new Euclid();   //exception error
  20.                     euc.methodEuclid(arrayArgs);  //exception error
  21.                }
  22.  
  23.              } catch (FileNotFoundException e) 
  24.               {
  25.                    e.printStackTrace(); 
  26.               }
  27.  
  28.              catch (IOException e) 
  29.            {
  30.             System.out.println("There was a problem creating/writing to the temp file");
  31.               e.printStackTrace();
  32.            }     
  33.     ................
  34.  
This is the Euclid class.
Expand|Select|Wrap|Line Numbers
  1. public class Euclid 
  2. {
  3.      public Euclid()throws Exception
  4.      {
  5.  
  6.      }
  7.  
  8.      public void methodEuclid(String[] arrayArgs) throws Exception 
  9.     {
  10.         int opcode = GCD; 
  11.  
  12.         BigInteger b1=null, b2=null;
  13.         String out="";
  14.  
  15.         b2 = receiveNumber(arrayArgs[1]);
  16.         b1 = receiveNumber(arrayArgs[0]);
  17.  
  18.          switch (opcode) 
  19.          {           
  20.             case GCD:       out = b1.gcd(b2).toString();
  21.                             break;
  22.             default:        throw new Exception("invalid operation");
  23.         }
  24.  
  25.           System.out.println(out);     }       
  26.  
  27.       private static final BigInteger receiveNumber(String str) throws Exception 
  28.     {
  29.         BigInteger b = null;
  30.         String thisLine = null;
  31.  
  32.         b = new BigInteger(str);  
  33.         return b;
  34.     }    
  35. }
Apr 27 '07 #6
shana07
281 100+
Basically exceptions come in two flavours: checked and unchecked exceptions.
The unchecked exceptions are thrown when a program error occurs such as
null pointers, division by zero, illegal arguments, array index out of bounds etc.

The compiler can't check on these exceptions so it doesn't. The checked
exceptions are usually external events that cause the program to fail such as
IO errors, broken socket connections etc. When a piece of code can throw
a checked exception it has to say so in its method header otherwise the
method can't throw such an exception. It can catch such exceptions and deal
with it (e.g. repair the situation, inform the user about it etc.)

The compiler checks your code for it: i.e. when your method can (in)directly
throw an exception it has to report it in its method header:
Expand|Select|Wrap|Line Numbers
  1. public void foo() throws FooExecption { ... }
If you fail to do that the compiler will flag that as a compilation error. Some
beginners are tempted to just write a "throws Exception" and forget about
the rest. This is a very bad habit because any other code calling your method
can't tell *what* exactly was thrown and act accordingly.

Exceptions must be part of the design, never just put "throws Exception"
at every method header when the compiler complains about it.

kind regards,

Jos
Jos, thank you. I assume checked and unchecked exceptions are refer to reported and unreported exceptions that I am having now. I still don't get, why some methods don't need throws Exception? How about main method?
Sorry, still not crystall clear about it...
Apr 27 '07 #7
JosAH
11,448 Expert 8TB
Jos, thank you. I assume checked and unchecked exceptions are refer to reported and unreported exceptions that I am having now. I still don't get, why some methods don't need throws Exception? How about main method?
Sorry, still not crystall clear about it...
Not really: there are two type of exceptions: checked and unchecked. For the
checked exceptions you either have to catch and handle them or *report* that
your method lets that exception pass to its caller(s). If you do neither the
compiler will tell you that you forgot to report the exception. Reporting is
simply adding a "throws SuchAndSoException" to the method declaration.

kind regards,

Jos
Apr 27 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Nicolas Fleury | last post by:
Hi, I've made a small utility to re-raise an exception with the same stack as before with additional information in it. Since I want to keep the same exception type and that some types have very...
1
by: Old Wolf | last post by:
1. What is the difference between #include <stdexcept> and #include <exception> ? 2. Is there a list somewhere of what each standard exception is used for? either to throw them, or throw...
11
by: Master of C++ | last post by:
Hi, I am writing a simulation package in C++, and so far I've written about 8000 lines of code and have about 30 classes. I haven't used C++ exceptions so far (for various reasons). The only two...
4
by: maricel | last post by:
I have the following base table structure - DDL: CREATE TABLE "ADMINISTRATOR"."T1" ( "C1" INTEGER NOT NULL ) IN "TEST_TS" ; ALTER TABLE "ADMINISTRATOR"."T1" ADD PRIMARY KEY
5
by: PCC | last post by:
I am using the Exception Managment Application Block on Windows Server 2003 Enterprise and .NET v1.1. If I use the block with an ASP.NET web wervice or in a web application I get the following...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
3
by: JohnDeHope3 | last post by:
First let me say that I understand that Asp.Net wraps my exception in an HttpUnhandledException. I have found a lot of discussion about that on the web, which was informative, but not helpful. Let...
4
by: cnixuser | last post by:
Hello, I am posting reguarding some modifications I made to a text editor program that I wrote in my java class at my school today. The file I was modifying is a very simple java text editor with...
2
by: Darko Miletic | last post by:
Recently I wrote a dll in c++ and to simplify the distribution I decided to link with multithreaded static library (/MT or /MTd option). In debug everything works fine but in release I get this: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.