Thread: [Any Revision] Handling Any Uncaught Exceptions

Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1 [Any Revision] Handling Any Uncaught Exceptions 
    Previously: Scary Kidz
    AceKingSuited's Avatar
    Join Date
    May 2010
    Posts
    506
    Thanks given
    126
    Thanks received
    79
    Rep Power
    69
    I was trying to find out a way to handle Exceptions without using a bunch of try and catch blocks and I came across something quite handy.

    This can be very useful if your server randomly disconnects, has random lags, random uncaught exceptions, etc. It can help you find the cause of the exceptions, write them to a file, or even handle them directly.

    Firstly, you need to go to your Main class, in my case (z508) , it's Server.java

    Somewhere in your main method, add this.

    Code:
    Thread.setDefaultUncaughtExceptionHandler(new ServerExceptionHandler());

    Now make a new class called ServerExceptionHandler and put this in it:

    Code:
    package net.com.codeusa;//Obviously, you are going to have to change.
    
    import java.io.*;//I don't like importing, and I wasn't using Eclipse atm.
    import java.util.*;
    import java.lang.*;
    import java.text.*;
    import java.lang.Thread.UncaughtExceptionHandler;
    
    /*
    * Written by Scary Kidz from Rune-Server
    */
    
    public class ServerExceptionHandler implements UncaughtExceptionHandler {
     
     public void uncaughtException(Thread thread, Throwable throwable) {
    	Calendar cal = Calendar.getInstance();
    	SimpleDateFormat sdf = new SimpleDateFormat("E MM/dd/yyyy 'at' hh:mm:ss a zzz");
        String t = sdf.format(cal.getTime());
    	throwable.printStackTrace();
    	System.out.println("Unhandled exception, logging it now..");
    	try	{
    		FileWriter fileStream=new FileWriter("./data/Exceptions.txt",true);
    		PrintWriter printWriter = new PrintWriter(fileStream);
    		printWriter.println(t);
    		throwable.printStackTrace(printWriter); 
    		printWriter.println();
    		printWriter.flush();
    		printWriter.close();
    		} catch (Exception e)	{
    		e.printStackTrace();
    		}
    //Currently just writes the Exceptions to a log. If you want to handle it, or do whatever, do it here. Note -  this doesn't include try and catch blocks.
    	}
    
    }

    Put it anywhere you want, just make sure you import it properly in your Main class.

    You can add..
    Code:
    Thread.setDefaultUncaughtExceptionHandler(new ServerExceptionHandler());
    To any threads in your server.

    I'm not sure if this is already implemented in any servers, I know Hyperion has a special logging system. I know it's common knowledge to a lot of people, but I've never seen it around, so hopefully it helps someone. If you use this, I wouldn't some feedback. If you have any tips on how to improve it, post 'em here and I might add it to the main post.
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    Jun 2009
    Posts
    106
    Thanks given
    72
    Thanks received
    25
    Rep Power
    40
    please read your visitor message nice tutorial
    Reply With Quote  
     

  3. #3  
    The internet's reject
    Andy's Avatar
    Join Date
    Jun 2007
    Age
    33
    Posts
    4,371
    Thanks given
    148
    Thanks received
    1,522
    Rep Power
    3133
    package net.com.codeusa;
    D'awwww people still use it.

    Anyway, useful to some, keep learning & creating.
    Reply With Quote  
     

  4. Thankful user:


  5. #4  
    Donator


    Join Date
    Sep 2006
    Age
    30
    Posts
    2,106
    Thanks given
    73
    Thanks received
    54
    Rep Power
    491
    cool really simple but very useful, I implemented this into my source thanks dude rep++
    Reply With Quote  
     

  6. Thankful user:


  7. #5  
    Previously: Scary Kidz
    AceKingSuited's Avatar
    Join Date
    May 2010
    Posts
    506
    Thanks given
    126
    Thanks received
    79
    Rep Power
    69
    It's nice to see people actually appreciate this. Thanks guys.
    Reply With Quote  
     

  8. #6  
    Certified Stoner

    demon dylan001's Avatar
    Join Date
    Feb 2009
    Age
    28
    Posts
    1,134
    Thanks given
    77
    Thanks received
    96
    Rep Power
    509
    oh so is this like used for when the cmd box gets spammed because of an error or something? this makes it show up in a text document instead of cmd box?
    Quote Originally Posted by Legitimate View Post
    this kids a str8 leecher
    lol ^ this kid calls me a leecher and buys tons of stuff from me.
    I host Call of duty prestige lobbies on xbox 360
    I can host any call of duty you want just make me an offer pm me if interested
    Spoiler for Sig too big:

    skype-demondylan001 discord demon dylan001#8904
    Attached image
    Reply With Quote  
     

  9. #7  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,876
    Thanks given
    508
    Thanks received
    1,898
    Rep Power
    5000
    only works for the main thread though
    never talk to me or my wife's son ever again
    Reply With Quote  
     

  10. #8  
    Registered Member

    Join Date
    Aug 2011
    Posts
    2,760
    Thanks given
    297
    Thanks received
    534
    Rep Power
    1596
    Quote Originally Posted by S747 View Post
    only works for the main thread though
    You can add..
    Code:
    Thread.setDefaultUncaughtExceptionHandler
    (new ServerExceptionHandler());
    To any threads in your server.
    Quote Originally Posted by Aj View Post
    This is not even a tutorial. It's fail for rep. It's fail for life.
    Reply With Quote  
     

  11. #9  
    Registered Member
    Join Date
    Apr 2012
    Posts
    13
    Thanks given
    0
    Thanks received
    2
    Rep Power
    11
    thanks for this very useful
    Reply With Quote  
     

  12. #10  
    Previously: Scary Kidz
    AceKingSuited's Avatar
    Join Date
    May 2010
    Posts
    506
    Thanks given
    126
    Thanks received
    79
    Rep Power
    69
    Quote Originally Posted by demon dylan001 View Post
    oh so is this like used for when the cmd box gets spammed because of an error or something? this makes it show up in a text document instead of cmd box?
    Well.. Yes and no. You can choose what to do with the Exception, so I had it write it to a file as an example. If you don't handle it, it will still print in the CMD box. Also, I purposely had it print in the CMD box anyways just so you know when it caught an unhandled Exception. Anything in your try and catch blocks will still be printed if you told it to, so they won't be printed unless you remove the blocks.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Two exceptions, need help.
    By baghori1 in forum Help
    Replies: 2
    Last Post: 01-19-2009, 06:53 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •