Some basic questions how to handle Exceptions with good style

Ok I have two really basic Exception questions at once:
1. Until now I always threw all exceptions back all the way to my main method and it always worked well for me but now I am sitting at a big project where this method would explode the programm with throwings of very many exceptions in very many methods. So I would like to know if there is a more elegant solution.
2. What do I do with exceptions that will never occur?
Lets say I have a method like this:
void main() {calculate();}
void calculate()
sum(3,5);
void sum(int a,int b)
MathematicsA.initialise("SUM"); // throws AlgorithmNotFoundException, will never occur but has to be handled
}So what is the most elegant way?:
h4. 1. Ignore because it will not happen
void sum(int a,int b)
try {MathematicsA.initialise("SUM");}
catch(AlgorithmNotFoundException) {}
}h4. 2. Print stacktrace and exit
void sum(int a,int b)
try {MathematicsA.initialise("SUM");}
catch(AlgorithmNotFoundException e)
e.printStackTrace();
System.exit();
}h4. 3. throw it everywhere
void main() throws AlgorithmNotFoundException, throws ThousandsOfOtherExceptions  {}
void calculate()  throws AlgorithmNotFoundException, throws HundretsOfOtherExceptions
sum(3,5);
void sum(int a,int b) throws AlgorithmNotFoundException
MathematicsA.initialise("SUM");
}h4. 4. Create special exceptions for every stage
void main() throws MainParametersWrongException
try {calculate();}
catch(Exception e)
  throw new MainParametersWrongException();
void calculate()  throws SumInternalErrorException
try {sum(3,5);}
catch (SumInternalErrorException)    {throw new CalculateException();}
void sum(int a,int b) throws SumInternalErrorException
try
MathematicsA.initialise("SUM");
} catch (AlgorithmNotFoundException e) {
throw new SumInternalErrorException();
}P.S.: Another problem for me is when a method called in a constructor causes an Exception. I don't want to do try/catch everytime I instantiate an object..
Example:
public class MySummation()
     Mathematics mathematics;
     public MySummation()
             mathematics.initialise("SUM"); // throws AlgorithmNotFoundException
     void sum(int x,int y)
          return mathematics.doIt(x,y);
}(sorry for editing all the time, I was not really sure what I really wanted to ask until i realised that I had in fact 2 questions at the same time, and still it is hard to explain what I really want to say with it but now the post is relatively final and I will only add small things)
Edited by: kirdie on Jul 7, 2008 2:21 AM
Edited by: kirdie on Jul 7, 2008 2:25 AM
Edited by: kirdie on Jul 7, 2008 2:33 AM
Edited by: kirdie on Jul 7, 2008 2:34 AM

sphinks wrote:
I`m not a guru, but give my point of view. First of all, the first way is rude. You shouldn`t use try with empty catch. "rude" isn't the word I'd use to describe it. Think about what happens if an exception is thrown. How will you know? Your app fails, and you have NO indication as to why. "stupid" or "suicidal" are better descriptions.
Then for the second way, I`ll reccomend for you use not printStackTrace(); , but use special method for it:
public void outputError (Exception e) {
e.printStackTrace();
}It`ll be better just because if in future you`d like to output error message instead of stack of print it on GUI, you`ll need change only one method, but not all 'try' 'catch' statements for changing e.printStackTrace(); with the call of new output method.I disagree with this. Far be it from me to argue against the DRY principle, but I wouldn't code it this way.
I would not recommend exiting from a catch block like that. It's not a strategy for recovery. Just throw the exception.
Then, the third way also good, but I suppose you should use throws only if the caller method should know that called method have a problem (for example, when you read parametrs from gui, so if params are inccorect, main method of programm should know about it and for example show alert window)."throw it everywhere"? No, throw it until you get to an appropriate handler.
If you're writing layered applications - and you should be - you want to identify the layer that will handle the exception. For example, if I'm writing a web app with a view layer, a service layer, and a persistence layer, and there's a problem in the persistence tier, I'd let the service layer know what the persistence exception was. The service layer might translate that into some other exception with some business meaning. The view layer would translate the business exception into something that would be actionable by the user.
And in all other cases I suppose you should use the fourth way. So I suppose it`s better to combine 2,3,4 ways as I have mentioned above.I don't know that I'd recommend special exceptions for every layer. I'd reuse existing classes where I could (e.g., IllegalArgumentException)
Sorry, I can give you advice how to avoid problem with try-catch in constructor. :-(You shouldn't catch anything that you can't handle. If an exception occurs in a constructor that will compromise the object you're trying to create, by all means just throw the exception. No try/catch needed.
You sound like you're looking for a single cookie cutter approach. I'd recommend thinking instead.
%

Similar Messages

  • Some basic questions on ESS and MSS

    Hi Experts,
    This is the first time I am going to deal with
    ESS and MSS.
    I have some basic questions about it and they are
    listed below:
    1. In an organization if they want to integrate
    ESS and MSS, how do they go about? What I mean
    is, will they install the packages in the R/3
    system and then use the to portal features to
    make iViews of ESS and MSS.
    2. Or is there a seperate package for the portal
    and the R/3 system
    3.What is the best way to customize ESS and MSS
    iViews? Is WebDynpro used to customize ESS and
    MSS applications, if yes how?
    4. Are these ESS and MSS packages different for
    ECC and the older version of R/3 systems? If yes what are the major differences?
    Please do let me know on these subjects and also
    if anyone of you have any links or documents
    associated with this scenario please do send me.
    Thank you
    Regards
    Luke

    Hi,
    here is what I know..
    1 & 2. Depending on the backend SAP version, the relevant ESS/MSS Business Packages have to be installed on the Portal. On the backend it will be via an Add-on.
    3. The webDynpro versions of ESS/MSS are available from mySAP ERP2004/2005 (ECC5/6). The best way to customize the iViews would be through the NetWeaver DevStudio.
    ent
    4. Yes, the ESS/MSS packages are differ depending on the backend SAP version.
    You can find all the  different packages & their info right here on SDN. From the home page click on the Portal Content Portfolio on the right hand side of the page>Browse Content Portfolio>Self Service for ESS & Line Manager for MSS.
    Regards,
    Suresh Datti

  • Some basic questions about DAC

    Hello
    I am just starting to use DAC now and I have some basic questions about DAC..
    1: How many different kinda image tables are there, what are they used for?
    2: In order to check which ETL mapping with in the same execution plan is taking a long time, what to do? I mean is there a table that stores such info?
    3: How to determine which ETL task to run after and which after?
    4: Is subject area always map to the Informatica workflow folder? Or what should subject area be?
    Please shed some lights on it, deeply appreciate!
    Thanks

    Hi,
    I have problems to understand you question but will do a try:
    From a logical point of view objects can be only serialized reasonabely when they have a state of it's own which can be reestablished at a later point in time and space without further dependencies.
    F.E. a "serialized file" would NOT make much sense cause it depends heavily on the state of the underlying file system.
    You can't expected that is can be deserialized at a later point of time (physical file refered may not longer exists) or on another machine, where the serialized object was transported to.
    Technical - at zero approach - it's more or less trivial. Simply implement Serializable and all non static, non transient fields of your object will be serialized automatically.
    But there is a couple of pitfalls, you should study documentations regarding this feature.
    Hope this helps
    Martin

  • How to handle dbms_xmldom with no data values.(no_data_found error in dom)

    hi,
    i have below block,
    DECLARE
    doc dbms_xmldom.DOMDocument;
    node dbms_xmldom.DOMNode;
    elem dbms_xmldom.DOMElement;
    cur_node dbms_xmldom.DOMNode;
    root_elem_data dbms_xmldom.DOMElement;
    root_elem_tab dbms_xmldom.DOMElement;
    root_node_data dbms_xmldom.DOMNode;
    mode_elmn dbms_xmldom.DOMElement;
    mode_node dbms_xmldom.DOMNode;
    mode_text dbms_xmldom.DOMText;
    doc1 DBMS_XMLDOM.DOMDOCUMENT;
    root_node_data1 DBMS_XMLDOM.DOMNODE;
    child_document DBMS_XMLDOM.DOMDOCUMENT;
    child_rootnode DBMS_XMLDOM.DOMNODE;
    V_CLOB CLOB;
    v_doc CLOB;
    v_EMP CLOB;
    v_output_filename VARCHAR2(300) := 'SPOOL_DIR/'||'EMP_XML_FILE.xml';
    l_xmltype XMLTYPE;
    BEGIN
    doc := dbms_xmldom.newDOMDocument;
    node := dbms_xmldom.makeNode(doc);
    dbms_xmldom.setversion(doc, '1.0');
    dbms_xmldom.setCharset(doc, 'UTF8');
    elem := dbms_xmldom.createElement(doc, 'PartnerInfo');
    dbms_xmldom.setAttribute(elem,'xmlns','EMP');
    cur_node := dbms_xmldom.appendChild(node, dbms_xmldom.makeNode(elem));
    mode_elmn := dbms_xmldom.createElement(doc, 'EMPLOYEE');
    mode_node := dbms_xmldom.appendChild(cur_node,dbms_xmldom.makeNode(mode_elmn));
    BEGIN
    SELECT value(e) INTO l_xmltype
    FROM TABLE(XMLSequence(Cursor(SELECT * FROM EMP1 where EMPNO=7501))) e;
    child_document := DBMS_XMLDOM.newDOMDocument(l_xmltype);
    root_node_data1 := dbms_xmldom.importNode(doc,dbms_xmldom.makeNode(dbms_xmldom.getDocumentElement(child_document)),TRUE);
    root_node_data1 := DBMS_XMLDOM.appendChild(root_node_data, root_node_data1);
    EXCEPTION
    WHEN OTHERS THEN
    Dbms_Output.Put_Line('Error in SELECT stmt(UC_PARTNER_MS):::'||'error::'||SQLERRM);
    END;
    dbms_lob.createtemporary(v_doc, true);
    dbms_xmldom.writeToClob(doc,v_doc,'UTF8');
    v_EMP:= v_doc;
    dbms_xmldom.writeToFile(DOC,v_output_filename,'UTF8');
    dbms_xmldom.freeDocument(doc);
    --Dbms_Output.Put_Line('THE OUTPUT IS::'||V_EMP);
    EXCEPTION
    WHEN OTHERS THEN
    Dbms_Output.Put_Line('Error in SELECT stmt(UC_PARTNER_MS):::'||'error::'||SQLERRM);
    END;
    The xml file is 'EMP_XML_FILE.xml'
    <empno>U++kYmcVuGchxbh+++++++++++++++1+</empno>
    <empname>J</empname>
    suppose the empno 7501 is not available in our emp table,
    i got error
    ORA-03113: end-of-file on communication channel
    how to handle xmldom with no data values.
    by
    siva

    hi,
    please give the solution
    by
    siva

  • How to handle exceptions in web dyn pro

    Hi Frndz....
                     Can any one kindly xplain how to handle exceptions in web dyn pro..like we hav exceptionhandling in java ....so is there any for NWDS
    THANKS & REGARDS
    Rajesh

    Hi,
    Web Dynpro Java is basically java only.i.e You program in java. Hence exceptions are supported in the same way as in java i.e. via try,catch and finally block.
    Additionally,
    In 04/04s if you create a method in any controller in web dynpro you cann't specify exception that can be thrown in the method. This is now possible in new programming web dynpro model in CE.
    Hence when creating a method you can also specify custom or core java (lib)excetions.
    Regards,
    Ashwani Kr Sharma

  • How to handle exception thrown in standard bo method in the workflow design

    Hi Experts
        how to handle exception thrown from standard bo method in the workflow design. For example, bo BUS2032, METHOD confirm. If the user cancel it, it will throw exception. In the workflow, how to catch this exception and add corresponding steps in the workflow.

    @jrockman li
    Try to implement the logic that what ever you are performing in the BO mehtod in a FM and in the FM you have tab with name EXECPTIONS define the execption in that tab.Now in the BO method you call this FM  and if the exception occurs by using RAISE you can raise the exception in the FM and based on the number of exceptions your sy-subrc value will be set
    so when sys-subrc is not eq 0 then pass a value back t the workflow container., I think this will work.
    a sample Snippet for understanding purpose
    CALL FUNCTION 'GUI_DOWNLOAD'
      EXPORTING
        filename         = <path>
        filetype         = 'ASC'
      IMPORTING
        filelength       = lv_len
      TABLES
        data_tab         = l_txt_tab
      EXCEPTIONS
        file_write_error = 1          " If this Exception occurs
        invalid_type     = 2
        no_authority     = 3
        unknown_error    = 4
        OTHERS           = 10.
    CASE sy-subrc.
      WHEN 1. " SY-SUBRC value will be 1 then,
          " Pass or set the value back to the workflow conatiner element
    ENDCASE.

  • How RMI handle exception in RMI kernel ?

    hi everyone.
    How RMI handle exception in RMI kernel ?
    That is , when RMI catch an exception ,
    what RMI should do to handle the exception?
    best regards.

    it throws an exception (some derived class of RemoteException) which is transmitted to, rethrown at, and must be caught at, the client.

  • Some basic question about SCM Installation

    Hi Folks,
    Could you please advice me about some basic questions about SCM and its installation -
    Q1. Do I need to have an R/3 System to be able to practice OR demo the SCM system?
    Q2. If, yes, which version of SCM is compatible with the ECC 5.0?
    Q3. What are the basic requirements to install SCM (version compatible with ECC 5.0) and ECC on a oracle db and windows OS?
    Q4. Does SCM have APO and BI as an inbuilt functionality OR does the user need to purchase these as an Add-On?
    I know these are some basic questions, but these will help me give me a direction.
    With best wishes,
    Krishna

    Hello Krishna -
    Q1. Do I need to have an R/3 System to be able to practice OR demo the SCM system?
    ---> There are various Modules in SCM Like APO (Sub Modules  DP (stand alone), SNP Needs Master data from R/3 and has to pass on transactional Data to R/3, PPDS same as SNP, GATP same as SNP), ICH needs Data from R/3 to work with. Core Interface (CIF) connects R/3 to APO for transferring data.
    Q2. If, yes, which version of SCM is compatible with the ECC 5.0?
    -->For thorough understanding explore the following links
    http://help.sap.com/saphelp_scm41/helpdata/en/9b/954d3baf755b67e10000000a114084/frameset.htm
    http://help.sap.com/saphelp_scm50/helpdata/en/9b/954d3baf755b67e10000000a114084/frameset.htm
    Q3. What are the basic requirements to install SCM (version compatible with ECC 5.0) and ECC on a oracle db and windows OS?
    -->Refer Above
    Q4. Does SCM have APO and BI as an inbuilt functionality OR does the user need to purchase these as an Add-On?
    --> APO comes with a small BI inbuilt functionality. you can only perform certain functions using that BI.
    Hope this helps.
    Regards,
    Suresh Garg

  • Some basic questions on HANA

    Hi All,
    I have some basic questions on SAP HANA,
    I heard from one of my colligues that it is mandatory to learn SQL scripts for HANA, is it true?
    Or basic knowledge is enough for HANA???
    Is it added advantage while working on HANA or can we manage with basic knowledge of SQL???
    I tried to find answers for these questions in SCN, but I could not get proper answers, So could you please provide answers for above my questions.
    Thanks in advance,
    Venkat Kalla

    Hi Vladmir,
    SAP HANA is a platform and is more than just database. Which can serve both OLTP and OLAP.
    But since it is a database, Sp SQL skills helps you while modelling in SAP HANA. So When will it be helpful ? It is while addressing some complex requirements using SAP HANA.
    At the end of the day, when you want to work in HANA ( a super duper database ) and you still thinking of whether to learn SQL or not?
    Regards,
    Krishna Tangudu

  • Some basic questions about serialization

    Hi,
    I have some basic questions on serialization.
    1. What are the basic criteria to make an object serializable(except that it must implement serializable ?) I mean that objects of some class inherently cannot be serialized so any object must have some features which makes it serializable/non serializable.
    2. I know it makes no sense to serialize a socket because it is fixed only by specifying ip and port at both ends, and is valid only when the network connection remains valid. Still in the context of my previous question, I would like to know why a socket cannot be serialized.

    Hi,
    I have problems to understand you question but will do a try:
    From a logical point of view objects can be only serialized reasonabely when they have a state of it's own which can be reestablished at a later point in time and space without further dependencies.
    F.E. a "serialized file" would NOT make much sense cause it depends heavily on the state of the underlying file system.
    You can't expected that is can be deserialized at a later point of time (physical file refered may not longer exists) or on another machine, where the serialized object was transported to.
    Technical - at zero approach - it's more or less trivial. Simply implement Serializable and all non static, non transient fields of your object will be serialized automatically.
    But there is a couple of pitfalls, you should study documentations regarding this feature.
    Hope this helps
    Martin

  • Some basic questions on File Adapter

    Hello all,
    I have some basic questions on XI and File Adapter and hope you can help me. Any answer is appreciated.
    1. Can I use NFS transport protocol to poll a file from a machine in the network, which is not the XI? Or do I have to use FTP instead?
    2. If I understand it correctly - when using the FTP-File Adapter, XI has the role of a ftp client. I have to run a ftp server on my distant machine. XI connects to FTP-Server and polls the file.
    Can it also be configured the other way round? The scenario I think of would be: FTP client installed on distant machine, which connects to FTP-Server(XI) and loads up a file. So XI would act as FTP Server.
    I know this works, if I install a ftp Server on the computer my XI runs on, and use the NFS-File Adapter to observe the folder. But I want to know, if I need a second, independant ftp server for this.
    3. And last but not least: When do I need the active ftp mode instead of passive?
    Thanx a lot for your answers!
    Ilona

    > Hello all,
    > I have some basic questions on XI and File Adapter
    > and hope you can help me. Any answer is appreciated.
    >
    >
    > 1. Can I use NFS transport protocol to poll a file
    > from a machine in the network, which is not the XI?
    <b>yes</b>
    > Or do I have to use FTP instead?
    >
    <b>also you can do it</b>
    > 2. If I understand it correctly - when using the
    > FTP-File Adapter, XI has the role of a ftp client. I
    > have to run a ftp server on my distant machine. XI
    > connects to FTP-Server and polls the file.
    > Can it also be configured the other way round? The
    > scenario I think of would be: FTP client installed on
    > distant machine, which connects to FTP-Server(XI) and
    > loads up a file. So XI would act as FTP Server.
    > I know this works, if I install a ftp Server on the
    > computer my XI runs on, and use the NFS-File Adapter
    > to observe the folder. But I want to know, if I need
    > a second, independant ftp server for this.
    >
    <b>XI cannot act as FTP server, but it is always a client. When XI is reading (File sender adpater) when XIis writing than it is File Receiver adapter</b>
    > 3. And last but not least: When do I need the active
    > ftp mode instead of passive?
    >
    <b>It depends on your firewall configuration. The best and the fastests is active mode but not always available.</b>
    > Thanx a lot for your answers!
    > Ilona

  • How to export RTF with paragraph style names?

    Anybody can help me "how to export RTF with paragraph style names?"
    Thank you

    It's not something that's natively done with Indesign.
    However it's possible that it might be able to be scripted.
    Check the scripting forum
    http://forums.adobe.com/community/indesign/indesign_scripting

  • Basic question, how to create rtf template with a given xml file

    Hello guys
    I am new to BI publisher and I am learning how to create rtf templates using given xml fiel definitions by following the bi publisher guide
    The steps I am following is:
    1, create template using template builders, which is downloaded into MS words.
    2, In the empty template, map the xml columns with template field using BI publisher tag
    3, upload the template to BI Publisher as a layout.
    That's all I can understand.
    My question is:
    1,in which above step do I import XML file into template builder (if not bi publisher), how to do so?
    2,After template is created, how to associate this template layout with existing reports in BI Publisher, or is it necessary at all?
    3,In the template content in MS Word template builder, what should I enter other than BI publisher tags for mapping XML field to desired template field
    Or, if my understand of this process is entirely wrong, what's the right process of creating rtf template with a given XML file definition
    Thanks

    If you have a BI Publisher desktop installed, have a look a the demos,.. should answer most of your basic questions.
    (Windows) Start->Programs->Oracle BI Publisher Desktop->Demos
    Invoice Demo - good one for those with E-Business Suite
    Report Demo - Sample using BI Publisher

  • Basic Questions On Scoring and LMS with Captivate 4

    Hello,
    Forive me if this question is too basic for this forum, but I have to ask it somewhere.
    I am an academic librarian who develops tutorials using Capitvate 4.  Each tutorial I develop has about 5 true/false, multiple choice questions.  My technical knowledge is limited as a "librarian," but is strong enough to storyboard, develop graphics, and write elearning tutorials. But I have no idea on how to  capture the results from my quizes and perform assessments, which is what I really need to do.
    So, basically, what do I need to do?  Do I ask my university to tap into their LMS?  Coordinate with someone in a techncial services capacity and let them handle it? Have test results emailed using Captivate?  Use something called Moodle? Wait until Captivate 5 comes out?  I do not need anything fancy; just percentages on pass/fail rates associated with 5 questions or so.
    Are there some basic instructions out there?
    Best,
    George Germek

    Hello,
    Since you seem to have a LMS, the easiest way is to let handle the assessment by the LMS. You'll have to ask where you can store the SCORM-objects(will explain soon) so that the users you want to reach can have access to them. The LMS takes care of the login for the users, and can link the results of an assessment to this user. You'll have to ask for the rights to have access to these results.
    Creating a SCORM-object from CP is possible, but you need to know which SCORM-standard is supported by the LMS. Then you will decide what you want to have reported from the LMS: p.e. only the score, if they failed/passed (when you have put in a mastery score), how many attempts they made etc. If you have this information, would be glad to help you set up a first SCORM-object from a CP-file.
    E-mailing results is a bit tricky in CP4, sometimes it works, sometimes not.
    In CP5 you'll have other possibilities than a LMS (by the way Moodle is a free LMS, do not think this will be OK for you because you already have a LMS around): posting the results to acrobat.com (rather simple process, really) or using an internal webserver (but then you'll need some technical assistance I'm afraid).
    Hope this gives you some ideas?
    Lilybiri
    PS: just realize this was your first posting. Welcome on this forum, George.

  • Mind answering some basic questions for a few bright HS students?

    I am a HS computer science teacher who seeks a professional mentor regarding JDBC. While I have successfully
    - installed a MySQL database,
    created, queried, updated the database,
    - read the Java Tutorial and:
    used Connect/J and written a main method to manipulate a
    database
    created very simple applets to query or update a database with
    their run methods.
    I have basic questions about how pieces fit together in web application development and how one can best take advantage of OOP when dealing with a database. My motivation are a few very bright post-AP students who have an excellent grasp of OOP and data structures; they are curious to see how Java is used in web applications.
    The sample code I have found on the Web has all been small main method examples. I would like to see and discuss a truly OOD application with JDBC, and to discuss how the pieces fit together.
    So, aware of our ignorance and of how dangerous a little knowledge can be, and not expecting to go live with an application anytime soon, we seek a very high level understanding of current professional practice (like: "Where does tomcat fit in here?") as well as some direction while we get our hands dirty. (I hope that makes sense.)
    If you have time to correspond with us via e-mail, answer some very basic questions, and perhaps take a look at some code as we work through it, we would love to hear from you off-line ([email protected]). We would also appreciate knowing where to look for a good OOD sample application. Thanks.

    Complete this tutorial and you will have a much better understanding.
    http://java.sun.com/javaee/5/docs/firstcup/doc/toc.html
    Also here is the tutorial for j2ee ( a little detailed but useful):
    http://java.sun.com/javaee/5/docs/tutorial/doc/

Maybe you are looking for