catch all program errors...

T

tiewknvc9

Im trying to find a quick way to catch all of my programs errors. These
would be the last remaining errors that are not currently caught in my
application,

for instance it would be a nullpointer exception - one that I never
expected to occur, so it was not caught. What I would like to do is to
catch any and all exceptions and log them in some way so that I can
update the program, and alert the user to the fact that an error had
occurred. The later I get, the catching all uncaught errors I am lost
with.

Any advice?

Thanks
 
P

Philipp Leitner

you can simply add a Try/Catch Block to your main method:

try {

// what you usually do in main

} catch (Exception e) {
// your logging code
}

Whether you /should/ do that is a completely different story, since you
are then surpressing severe program errors (what does not make them
less severe, but harder to detect). For example the program might throw
your NullPointerException in the middle of some transaction with the
user; then the exception is caught, and the user might never notice
that the transaction was not finished. The application might be in an
undefined error state then.

I would think about the risks first.

/philipp
 
D

Daniel Dyer

you can simply add a Try/Catch Block to your main method:

try {

// what you usually do in main

} catch (Exception e) {
// your logging code
}

That will only work for exceptions on the main thread. To catch
exceptions on all threads take a look at:

http://java.sun.com/j2se/1.5.0/docs...er(java.lang.Thread.UncaughtExceptionHandler)

and

http://java.sun.com/j2se/1.5.0/docs...er(java.lang.Thread.UncaughtExceptionHandler)
Whether you /should/ do that is a completely different story, since you
are then surpressing severe program errors (what does not make them
less severe, but harder to detect). For example the program might throw
your NullPointerException in the middle of some transaction with the
user; then the exception is caught, and the user might never notice
that the transaction was not finished. The application might be in an
undefined error state then.

Well if these exceptions are escaping unhandled, something is broken
anyway. And there is nothing stopping you from rethrowing the exception..

Dan.
 
T

tiewknvc9

ok, I read about the threads and have actually worked with threads
previously.

To clarify, I have created a well rounded, well tested application. I
do not expect any errors to be thrown, however in certain instances I
am sure that it is possible, simply because I am sure that I am not
flawless.

Yet I want to be sure that the user does not continue to use a program
that is not reacting correctly, one minor error can cause the program
to react poorly in time. So I think that the way I am going to go is
to make every single one of my functions throw an Exception. This way
I can react to the problem in a case by case basis.

Unless this seems like overkill or something. It is a lot of work, but
just to be sure that something worthless (like auto-updating text) does
not cause the entire program to appear to crash.... What do you guys
think, is that a good way to approach this?
 
O

Oliver Wong

tiewknvc9 said:
Yet I want to be sure that the user does not continue to use a program
that is not reacting correctly, one minor error can cause the program
to react poorly in time. So I think that the way I am going to go is
to make every single one of my functions throw an Exception. This way
I can react to the problem in a case by case basis.

I don't know what you mean by "react poorly in time" (cause the program
to slow down?), but this is probably a very bad idea. If your get runtime
exceptions thrown from within your code, it means there's a bug in your
code. Adding more code won't solve the problem. The solution is to fix the
bug itself.

You might want to read
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

- Oliver
 
S

steve

Im trying to find a quick way to catch all of my programs errors. These
would be the last remaining errors that are not currently caught in my
application,

for instance it would be a nullpointer exception - one that I never
expected to occur, so it was not caught. What I would like to do is to
catch any and all exceptions and log them in some way so that I can
update the program, and alert the user to the fact that an error had
occurred. The later I get, the catching all uncaught errors I am lost
with.

Any advice?

Thanks

re-point the con out to you logger., (do it VERY early in your code)

public class consoleIntercept extends PrintStream {
private PrintStream console = System.out;
private PrintStream err = System.err;


public consoleIntercept() {
super(System.out, true); // Autoflush
System.setOut(this);
System.setErr(this);

}


public void dispose() {
System.setOut(console);
System.setErr(err);


}
public void println(Object x) {

Error_stuff.handleError(x.toString());
}

}


I have 2 error systems , one that traps and dumps inside try{}catch{}
blocks, and then the above "catch all"

an example is below (where stuff is being written to the console , that
should not be, but obviously it catches dumps as well)

2006-05-23 07:25:34,353 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:37:31,634 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:42:04,457 ERROR com.Errors -
INTERCEPT----[Ljava.lang.Object;@a7166f
2006-05-23 07:54:09,947 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:59:42,787 ERROR com.Errors -
INTERCEPT----com.RecordStructures.SupplierRecordStructure@f45223
2006-05-23 17:36:38,483 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 17:37:10,108 ERROR com.Errors - INTERCEPT----dispatch watcher
expired


and now a "dump" of a missing class

2006-05-19 18:23:07,885 ERROR com.Errors - INTERCEPT----Exception Caused when
trying to Execute main Class
2006-05-19 18:23:07,896 ERROR com.Errors -
INTERCEPT----java.lang.reflect.InvocationTargetException
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.j
ava:25)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
java.lang.reflect.Method.invoke(Method.java:324)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
bootLoader.bootLoader$3.run(bootLoader.java:199)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
java.lang.Thread.run(Thread.java:552)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT----Caused by:
java.lang.NoClassDefFoundError: oracle/jdbc/pool/OracleDataSource
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- at
com.sql.SQL_stuff.Open_odbc(SQL_stuff.java:2364)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- at
RootClassFrame.RootClassFrame.main(RootClassFrame.java:376)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- ... 6 more
2006-05-19 18:55:25,997 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
 
L

Larry Barowski

tiewknvc9 said:
Unless this seems like overkill or something. It is a lot of work, but
just to be sure that something worthless (like auto-updating text) does
not cause the entire program to appear to crash.... What do you guys
think, is that a good way to approach this?

You have no way to know that an exception in "something worthless"
did not corrupt underlying data. The best thing to do with an
unexpected exception is allow the user to send the stack dump
through an automatic bug-reporting system, possibly attempt to back
up user data (but do not overwrite any existing files), and quit.
 
K

Knute Johnson

Oliver said:
I don't know what you mean by "react poorly in time" (cause the
program to slow down?), but this is probably a very bad idea. If your
get runtime exceptions thrown from within your code, it means there's a
bug in your code. Adding more code won't solve the problem. The solution
is to fix the bug itself.

You might want to read
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

- Oliver

There are often times that you don't want your program to stop when a
runtime exception occurs. Catching them is simple enough. The program
should report the errors to the user or log them for repair at some
future time. Exception.printStackTrace() is your friend :).
 
O

Oliver Wong

Knute Johnson said:
There are often times that you don't want your program to stop when a
runtime exception occurs. Catching them is simple enough. The program
should report the errors to the user or log them for repair at some future
time. Exception.printStackTrace() is your friend :).

I'm not sure if you're supporting my view, or if you're against it, but
my main objection is declaring every single function to throw Exception.
(Just to clarify).

- Oliver
 
T

tiewknvc9

ok, here is where I am.

I created a class that extends OutputStream, and I have directed all
system errors to output to that stream via

System.setErr(new PrintStream(myOsErr))

the output stream now writes all errors to a file - a nice and happy
success :)

although it writes the errors one character at a time.

Now the issue remains... how can I detect when the error occurs, and
offer the user the ability to save their work and send the error file
to me?

Im trying to find a quick way to catch all of my programs errors. These
would be the last remaining errors that are not currently caught in my
application,

for instance it would be a nullpointer exception - one that I never
expected to occur, so it was not caught. What I would like to do is to
catch any and all exceptions and log them in some way so that I can
update the program, and alert the user to the fact that an error had
occurred. The later I get, the catching all uncaught errors I am lost
with.

Any advice?

Thanks

re-point the con out to you logger., (do it VERY early in your code)

public class consoleIntercept extends PrintStream {
private PrintStream console = System.out;
private PrintStream err = System.err;


public consoleIntercept() {
super(System.out, true); // Autoflush
System.setOut(this);
System.setErr(this);

}


public void dispose() {
System.setOut(console);
System.setErr(err);


}
public void println(Object x) {

Error_stuff.handleError(x.toString());
}

}


I have 2 error systems , one that traps and dumps inside try{}catch{}
blocks, and then the above "catch all"

an example is below (where stuff is being written to the console , that
should not be, but obviously it catches dumps as well)

2006-05-23 07:25:34,353 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:37:31,634 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:42:04,457 ERROR com.Errors -
INTERCEPT----[Ljava.lang.Object;@a7166f
2006-05-23 07:54:09,947 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:59:42,787 ERROR com.Errors -
INTERCEPT----com.RecordStructures.SupplierRecordStructure@f45223
2006-05-23 17:36:38,483 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 17:37:10,108 ERROR com.Errors - INTERCEPT----dispatch watcher
expired


and now a "dump" of a missing class

2006-05-19 18:23:07,885 ERROR com.Errors - INTERCEPT----Exception Caused when
trying to Execute main Class
2006-05-19 18:23:07,896 ERROR com.Errors -
INTERCEPT----java.lang.reflect.InvocationTargetException
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.j
ava:25)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
java.lang.reflect.Method.invoke(Method.java:324)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
bootLoader.bootLoader$3.run(bootLoader.java:199)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
java.lang.Thread.run(Thread.java:552)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT----Caused by:
java.lang.NoClassDefFoundError: oracle/jdbc/pool/OracleDataSource
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- at
com.sql.SQL_stuff.Open_odbc(SQL_stuff.java:2364)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- at
RootClassFrame.RootClassFrame.main(RootClassFrame.java:376)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- ... 6 more
2006-05-19 18:55:25,997 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
 
K

Knute Johnson

Oliver said:
I'm not sure if you're supporting my view, or if you're against it,
but my main objection is declaring every single function to throw
Exception. (Just to clarify).

- Oliver

I agree, declaring every method to throw Exception is a bad idea. What
I meant was you might want to catch some of them so the program doesn't
blow up. The method or part of it anyway will be rendered useless if it
throws an Exception but maybe the next execution will run successfully.
 
K

Knute Johnson

tiewknvc9 said:
ok, here is where I am.

I created a class that extends OutputStream, and I have directed all
system errors to output to that stream via

System.setErr(new PrintStream(myOsErr))

the output stream now writes all errors to a file - a nice and happy
success :)

although it writes the errors one character at a time.

Now the issue remains... how can I detect when the error occurs, and
offer the user the ability to save their work and send the error file
to me?

Im trying to find a quick way to catch all of my programs errors. These
would be the last remaining errors that are not currently caught in my
application,

for instance it would be a nullpointer exception - one that I never
expected to occur, so it was not caught. What I would like to do is to
catch any and all exceptions and log them in some way so that I can
update the program, and alert the user to the fact that an error had
occurred. The later I get, the catching all uncaught errors I am lost
with.

Any advice?

Thanks
re-point the con out to you logger., (do it VERY early in your code)

public class consoleIntercept extends PrintStream {
private PrintStream console = System.out;
private PrintStream err = System.err;


public consoleIntercept() {
super(System.out, true); // Autoflush
System.setOut(this);
System.setErr(this);

}


public void dispose() {
System.setOut(console);
System.setErr(err);


}
public void println(Object x) {

Error_stuff.handleError(x.toString());
}

}


I have 2 error systems , one that traps and dumps inside try{}catch{}
blocks, and then the above "catch all"

an example is below (where stuff is being written to the console , that
should not be, but obviously it catches dumps as well)

2006-05-23 07:25:34,353 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:37:31,634 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:42:04,457 ERROR com.Errors -
INTERCEPT----[Ljava.lang.Object;@a7166f
2006-05-23 07:54:09,947 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:59:42,787 ERROR com.Errors -
INTERCEPT----com.RecordStructures.SupplierRecordStructure@f45223
2006-05-23 17:36:38,483 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 17:37:10,108 ERROR com.Errors - INTERCEPT----dispatch watcher
expired


and now a "dump" of a missing class

2006-05-19 18:23:07,885 ERROR com.Errors - INTERCEPT----Exception Caused when
trying to Execute main Class
2006-05-19 18:23:07,896 ERROR com.Errors -
INTERCEPT----java.lang.reflect.InvocationTargetException
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.j
ava:25)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
java.lang.reflect.Method.invoke(Method.java:324)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
bootLoader.bootLoader$3.run(bootLoader.java:199)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
java.lang.Thread.run(Thread.java:552)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT----Caused by:
java.lang.NoClassDefFoundError: oracle/jdbc/pool/OracleDataSource
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- at
com.sql.SQL_stuff.Open_odbc(SQL_stuff.java:2364)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- at
RootClassFrame.RootClassFrame.main(RootClassFrame.java:376)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- ... 6 more
2006-05-19 18:55:25,997 ERROR com.Errors - INTERCEPT----dispatch watcher
expired

try {
// your method here
} catch (SomeException) {
// handle normal exception
} catch (SomeOtherException) {
// handle other normal exception
} catch (Exception) {
// this is for unexpected exceptions
// write your error message to a file, or
// connect to your server and send you the message and
// display a message to the user that an error occurred
}
 
T

tiewknvc9

oh! ok! great idea.. I didnt remember that I could just connect to my
own server and write the problems occurred directly there!

Thanks!


Knute said:
tiewknvc9 said:
ok, here is where I am.

I created a class that extends OutputStream, and I have directed all
system errors to output to that stream via

System.setErr(new PrintStream(myOsErr))

the output stream now writes all errors to a file - a nice and happy
success :)

although it writes the errors one character at a time.

Now the issue remains... how can I detect when the error occurs, and
offer the user the ability to save their work and send the error file
to me?

Im trying to find a quick way to catch all of my programs errors. These
would be the last remaining errors that are not currently caught in my
application,

for instance it would be a nullpointer exception - one that I never
expected to occur, so it was not caught. What I would like to do is to
catch any and all exceptions and log them in some way so that I can
update the program, and alert the user to the fact that an error had
occurred. The later I get, the catching all uncaught errors I am lost
with.

Any advice?

Thanks

re-point the con out to you logger., (do it VERY early in your code)

public class consoleIntercept extends PrintStream {
private PrintStream console = System.out;
private PrintStream err = System.err;


public consoleIntercept() {
super(System.out, true); // Autoflush
System.setOut(this);
System.setErr(this);

}


public void dispose() {
System.setOut(console);
System.setErr(err);


}
public void println(Object x) {

Error_stuff.handleError(x.toString());
}

}


I have 2 error systems , one that traps and dumps inside try{}catch{}
blocks, and then the above "catch all"

an example is below (where stuff is being written to the console , that
should not be, but obviously it catches dumps as well)

2006-05-23 07:25:34,353 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:37:31,634 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:42:04,457 ERROR com.Errors -
INTERCEPT----[Ljava.lang.Object;@a7166f
2006-05-23 07:54:09,947 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 07:59:42,787 ERROR com.Errors -
INTERCEPT----com.RecordStructures.SupplierRecordStructure@f45223
2006-05-23 17:36:38,483 ERROR com.Errors - INTERCEPT----dispatch watcher
expired
2006-05-23 17:37:10,108 ERROR com.Errors - INTERCEPT----dispatch watcher
expired


and now a "dump" of a missing class

2006-05-19 18:23:07,885 ERROR com.Errors - INTERCEPT----Exception Caused when
trying to Execute main Class
2006-05-19 18:23:07,896 ERROR com.Errors -
INTERCEPT----java.lang.reflect.InvocationTargetException
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
2006-05-19 18:23:07,898 ERROR com.Errors - INTERCEPT---- at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.j
ava:25)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
java.lang.reflect.Method.invoke(Method.java:324)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
bootLoader.bootLoader$3.run(bootLoader.java:199)
2006-05-19 18:23:07,899 ERROR com.Errors - INTERCEPT---- at
java.lang.Thread.run(Thread.java:552)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT----Caused by:
java.lang.NoClassDefFoundError: oracle/jdbc/pool/OracleDataSource
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- at
com.sql.SQL_stuff.Open_odbc(SQL_stuff.java:2364)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- at
RootClassFrame.RootClassFrame.main(RootClassFrame.java:376)
2006-05-19 18:23:07,900 ERROR com.Errors - INTERCEPT---- ... 6 more
2006-05-19 18:55:25,997 ERROR com.Errors - INTERCEPT----dispatch watcher
expired

try {
// your method here
} catch (SomeException) {
// handle normal exception
} catch (SomeOtherException) {
// handle other normal exception
} catch (Exception) {
// this is for unexpected exceptions
// write your error message to a file, or
// connect to your server and send you the message and
// display a message to the user that an error occurred
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top