Java Throwing Exception - Best Practise









up vote
-1
down vote

favorite












What is the best practice to throw the error message?



As soon as I throw the exception, the exception has been traced in the errorlog.log file. I have more custom exception. If I throw these exceptions, for all the incorrect attempt, it will be traced in the logfile and it bugs the file and increasing the log file size.



what is the good way of doing this? Is it mandatory in java that we have to do the programming like this?



I want most of my custom messages like single line of error and very critical error should be thrown with complete error trace.



Need suggestion on error handling and tracing the log.










share|improve this question

















  • 3




    You should look into the logging frameworks like slf4j or log4j. They offer different logging levels, which exceptions might trigger. A severe message would only be logged for very serious exceptions.
    – Tim Biegeleisen
    Nov 10 at 14:11










  • It’s not a rule of life that every exception thrown is logged. I’d rather say it’s bad practice to so indiscriminately. I don’t know why your exceptions are, but it must come from a mechanism that is specific to your system.
    – Ole V.V.
    Nov 10 at 14:47











  • In general what is the behavior if I throw an exception. It will print the logs in the console right? Instead, I want just one line message. How this could be done using throw? If I catch, I can use exception.getmessage. If I throw, the entire trace is getting printed.
    – Shakthi
    Nov 10 at 16:10










  • Without knowing your setup we can’t tell. No, a program only produces a log if the code explicitly does so, and the only things that go into the log are those that the code writes into it.
    – Ole V.V.
    Nov 11 at 7:06














up vote
-1
down vote

favorite












What is the best practice to throw the error message?



As soon as I throw the exception, the exception has been traced in the errorlog.log file. I have more custom exception. If I throw these exceptions, for all the incorrect attempt, it will be traced in the logfile and it bugs the file and increasing the log file size.



what is the good way of doing this? Is it mandatory in java that we have to do the programming like this?



I want most of my custom messages like single line of error and very critical error should be thrown with complete error trace.



Need suggestion on error handling and tracing the log.










share|improve this question

















  • 3




    You should look into the logging frameworks like slf4j or log4j. They offer different logging levels, which exceptions might trigger. A severe message would only be logged for very serious exceptions.
    – Tim Biegeleisen
    Nov 10 at 14:11










  • It’s not a rule of life that every exception thrown is logged. I’d rather say it’s bad practice to so indiscriminately. I don’t know why your exceptions are, but it must come from a mechanism that is specific to your system.
    – Ole V.V.
    Nov 10 at 14:47











  • In general what is the behavior if I throw an exception. It will print the logs in the console right? Instead, I want just one line message. How this could be done using throw? If I catch, I can use exception.getmessage. If I throw, the entire trace is getting printed.
    – Shakthi
    Nov 10 at 16:10










  • Without knowing your setup we can’t tell. No, a program only produces a log if the code explicitly does so, and the only things that go into the log are those that the code writes into it.
    – Ole V.V.
    Nov 11 at 7:06












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











What is the best practice to throw the error message?



As soon as I throw the exception, the exception has been traced in the errorlog.log file. I have more custom exception. If I throw these exceptions, for all the incorrect attempt, it will be traced in the logfile and it bugs the file and increasing the log file size.



what is the good way of doing this? Is it mandatory in java that we have to do the programming like this?



I want most of my custom messages like single line of error and very critical error should be thrown with complete error trace.



Need suggestion on error handling and tracing the log.










share|improve this question













What is the best practice to throw the error message?



As soon as I throw the exception, the exception has been traced in the errorlog.log file. I have more custom exception. If I throw these exceptions, for all the incorrect attempt, it will be traced in the logfile and it bugs the file and increasing the log file size.



what is the good way of doing this? Is it mandatory in java that we have to do the programming like this?



I want most of my custom messages like single line of error and very critical error should be thrown with complete error trace.



Need suggestion on error handling and tracing the log.







java exception error-handling






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 14:09









Shakthi

143213




143213







  • 3




    You should look into the logging frameworks like slf4j or log4j. They offer different logging levels, which exceptions might trigger. A severe message would only be logged for very serious exceptions.
    – Tim Biegeleisen
    Nov 10 at 14:11










  • It’s not a rule of life that every exception thrown is logged. I’d rather say it’s bad practice to so indiscriminately. I don’t know why your exceptions are, but it must come from a mechanism that is specific to your system.
    – Ole V.V.
    Nov 10 at 14:47











  • In general what is the behavior if I throw an exception. It will print the logs in the console right? Instead, I want just one line message. How this could be done using throw? If I catch, I can use exception.getmessage. If I throw, the entire trace is getting printed.
    – Shakthi
    Nov 10 at 16:10










  • Without knowing your setup we can’t tell. No, a program only produces a log if the code explicitly does so, and the only things that go into the log are those that the code writes into it.
    – Ole V.V.
    Nov 11 at 7:06












  • 3




    You should look into the logging frameworks like slf4j or log4j. They offer different logging levels, which exceptions might trigger. A severe message would only be logged for very serious exceptions.
    – Tim Biegeleisen
    Nov 10 at 14:11










  • It’s not a rule of life that every exception thrown is logged. I’d rather say it’s bad practice to so indiscriminately. I don’t know why your exceptions are, but it must come from a mechanism that is specific to your system.
    – Ole V.V.
    Nov 10 at 14:47











  • In general what is the behavior if I throw an exception. It will print the logs in the console right? Instead, I want just one line message. How this could be done using throw? If I catch, I can use exception.getmessage. If I throw, the entire trace is getting printed.
    – Shakthi
    Nov 10 at 16:10










  • Without knowing your setup we can’t tell. No, a program only produces a log if the code explicitly does so, and the only things that go into the log are those that the code writes into it.
    – Ole V.V.
    Nov 11 at 7:06







3




3




You should look into the logging frameworks like slf4j or log4j. They offer different logging levels, which exceptions might trigger. A severe message would only be logged for very serious exceptions.
– Tim Biegeleisen
Nov 10 at 14:11




You should look into the logging frameworks like slf4j or log4j. They offer different logging levels, which exceptions might trigger. A severe message would only be logged for very serious exceptions.
– Tim Biegeleisen
Nov 10 at 14:11












It’s not a rule of life that every exception thrown is logged. I’d rather say it’s bad practice to so indiscriminately. I don’t know why your exceptions are, but it must come from a mechanism that is specific to your system.
– Ole V.V.
Nov 10 at 14:47





It’s not a rule of life that every exception thrown is logged. I’d rather say it’s bad practice to so indiscriminately. I don’t know why your exceptions are, but it must come from a mechanism that is specific to your system.
– Ole V.V.
Nov 10 at 14:47













In general what is the behavior if I throw an exception. It will print the logs in the console right? Instead, I want just one line message. How this could be done using throw? If I catch, I can use exception.getmessage. If I throw, the entire trace is getting printed.
– Shakthi
Nov 10 at 16:10




In general what is the behavior if I throw an exception. It will print the logs in the console right? Instead, I want just one line message. How this could be done using throw? If I catch, I can use exception.getmessage. If I throw, the entire trace is getting printed.
– Shakthi
Nov 10 at 16:10












Without knowing your setup we can’t tell. No, a program only produces a log if the code explicitly does so, and the only things that go into the log are those that the code writes into it.
– Ole V.V.
Nov 11 at 7:06




Without knowing your setup we can’t tell. No, a program only produces a log if the code explicitly does so, and the only things that go into the log are those that the code writes into it.
– Ole V.V.
Nov 11 at 7:06

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239780%2fjava-throwing-exception-best-practise%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239780%2fjava-throwing-exception-best-practise%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線